Skip to content

Add support for alternate keys #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhotze opened this issue Oct 11, 2021 · 0 comments · Fixed by #293 or #308
Closed

Add support for alternate keys #120

mhotze opened this issue Oct 11, 2021 · 0 comments · Fixed by #293 or #308
Assignees
Labels
priority:p2 Medium. Generally has a work-around and a smaller sub-set of customers is affected. SLA <=30 days type:enhancement Enhancement request targeting an existing experience.
Milestone

Comments

@mhotze
Copy link

mhotze commented Oct 11, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority:p2 Medium. Generally has a work-around and a smaller sub-set of customers is affected. SLA <=30 days type:enhancement Enhancement request targeting an existing experience.
Projects
None yet
4 participants