-
Notifications
You must be signed in to change notification settings - Fork 64
Description
Models containing alternate keys cannot easily be converted to OpenAPI. Corresponding paths provided by a custom path provider lead to an unhandled exception in parameter creation.
Assemblies affected
All versions of Microsoft.OpenApi.OData.Reader.dll
Steps to reproduce
See the following modifications of the single key test. It reflects a case for an alternate key path http://host/service/Customer(AlternateId='A1245') which was added by a custom ODataPathProvider handed over in the OpenAPIConvertSettings. The same can be achieved for multi-part alternate keys.
[Theory]
[InlineData(true)]
[InlineData(false)]
public void CreateKeyParametersForSingleKeyWorks(bool prefix)
{
// Arrange
EdmModel model = new EdmModel();
EdmEntityType customer = new EdmEntityType("NS", "Customer");
customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.String));
+ var alternateId = customer.AddStructuralProperty("AlternateId", EdmPrimitiveTypeKind.String);
+ model.AddAlternateKeyAnnotation(customer, new System.Collections.Generic.Dictionary<string, IEdmProperty> { { "AlternateId", alternateId } });
model.AddElement(customer);
OpenApiConvertSettings setting = new OpenApiConvertSettings
{
PrefixEntityTypeNameBeforeKey = prefix
};
ODataContext context = new ODataContext(model, setting);
ODataKeySegment keySegment = new ODataKeySegment(customer);
// Act
+ var parameters = context.CreateKeyParameters(keySegment, new System.Collections.Generic.Dictionary<string, string> { { "AlternateId", "alternateId" } });
- var parameters = context.CreateKeyParameters(keySegment);
...Expected result
No exception without considering alternate keys.
Actual result
KeyNotFoundException in OpenApiParameterGenerator.CreateKeyParameters
Additional detail
Given the mere example http://host/service/Employees(ID='A1245') from OData Version 4.01. Part 2: URL Conventions, without a model it is unclear whether one should rely on the (primary) key or an alternate key. Since the model is known for the generator, not only the primary key but also the alternative keys could be taken into account when mapping the parameter names. Definitely it should work fault tolerant with any other PathProvider then the default ODataPathProvider.