Skip to content

Add support for alternate key parameters #293

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

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(keySegment, nameof(keySegment));

IList<OpenApiParameter> parameters = new List<OpenApiParameter>();
List<OpenApiParameter> parameters = new();
IEdmEntityType entityType = keySegment.EntityType;

IList<IEdmStructuralProperty> keys = entityType.Key().ToList();
if (keys.Count() == 1)
{
Expand Down Expand Up @@ -212,6 +212,46 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
}
}

IList<OpenApiParameter> alternateKeyParameters = CreateAlternateKeyParameters(entityType, context);
parameters.AddRange(alternateKeyParameters);

return parameters;
}

private static IList<OpenApiParameter> CreateAlternateKeyParameters(IEdmEntityType entityType, ODataContext context)
{
IList<OpenApiParameter> parameters = new List<OpenApiParameter>();
IEnumerable<IDictionary<string, IEdmProperty>> alternateKeys = context.Model.GetAlternateKeysAnnotation(entityType);
foreach (var alternateKey in alternateKeys)
{
if (alternateKey.Count() == 1)
{
parameters.Add(
new OpenApiParameter
{
Name = alternateKey.First().Key,
In = ParameterLocation.Path,
Description = $"Alternate key: {alternateKey.First().Value.Name} of {entityType.Name}",
Schema = context.CreateEdmTypeSchema(alternateKey.First().Value.Type)
}
);
}
else
{
foreach (var compositekey in alternateKey)
{
parameters.Add(
new OpenApiParameter
{
Name = compositekey.Key,
In = ParameterLocation.Path,
Description = $"Composite alternate key: {compositekey.Value.Name} of {entityType.Name}",
Schema = context.CreateEdmTypeSchema(compositekey.Value.Type)
}
);
}
}
}
return parameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PackageId>Microsoft.OpenApi.OData</PackageId>
<SignAssembly>true</SignAssembly>
<Version>1.2.0-preview2</Version>
<Version>1.2.0-preview3</Version>
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
<RepositoryUrl>https://github.com/Microsoft/OpenAPI.NET.OData</RepositoryUrl>
<PackageReleaseNotes>
- Use convert setting to toggle between referencing @odata.count and @odata.nextLink #282
- Fixes URL Path parameters of type datetime generated as strings with quotes #262
- Add support for alternate keys #120
</PackageReleaseNotes>
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,95 @@ public void CreateKeyParametersForCompositeKeyWorks(bool prefix)
Assert.Equal(expected.ChangeLineBreaks(), json);
}

[Fact]
public void CreateKeyParametersForAlternateKeyWithSinglePropertyWorks()
{
// Arrange
EdmModel model = new EdmModel();
EdmEntityType customer = new EdmEntityType("NS", "Customer");
customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.String));

IEdmProperty alternateId = customer.AddStructuralProperty("AlternateId", EdmPrimitiveTypeKind.String);
model.AddAlternateKeyAnnotation(customer, new Dictionary<string, IEdmProperty> { { "AltId", alternateId } });

model.AddElement(customer);
ODataContext context = new(model);
ODataKeySegment keySegment = new(customer);

// Act
var parameters = context.CreateKeyParameters(keySegment);
var altParameter = parameters.Last();

// Assert
Assert.NotNull(parameters);
Assert.Equal(2, parameters.Count);
string json = altParameter.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
Assert.Equal(@"{
""name"": ""AltId"",
""in"": ""path"",
""description"": ""Alternate key: AlternateId of Customer"",
""style"": ""simple"",
""schema"": {
""type"": ""string"",
""nullable"": true
}
}".ChangeLineBreaks(), json);
}

[Fact]
public void CreateKeyParametersForAlternateKeyWithMultiplePropertiesWorks()
{
// Arrange
EdmModel model = new EdmModel();
EdmEntityType customer = new EdmEntityType("NS", "Customer");
customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.String));

IEdmProperty alternateId1 = customer.AddStructuralProperty("AlternateId1", EdmPrimitiveTypeKind.String);
IEdmProperty alternateId2 = customer.AddStructuralProperty("AlternateId2", EdmPrimitiveTypeKind.String);
model.AddAlternateKeyAnnotation(customer,
new Dictionary<string, IEdmProperty>
{
{ "AltId1", alternateId1 },
{ "AltId2", alternateId2 }
});

model.AddElement(customer);
ODataContext context = new(model);
ODataKeySegment keySegment = new(customer);

// Act
var parameters = context.CreateKeyParameters(keySegment);
var altParameter1 = parameters.Skip(1).First();
var altParameter2 = parameters.Last();

// Assert
Assert.NotNull(parameters);
Assert.Equal(3, parameters.Count);
string json1 = altParameter1.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
Assert.Equal(@"{
""name"": ""AltId1"",
""in"": ""path"",
""description"": ""Composite alternate key: AlternateId1 of Customer"",
""style"": ""simple"",
""schema"": {
""type"": ""string"",
""nullable"": true
}
}".ChangeLineBreaks(), json1);

string json2 = altParameter2.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
Assert.Equal(@"{
""name"": ""AltId2"",
""in"": ""path"",
""description"": ""Composite alternate key: AlternateId2 of Customer"",
""style"": ""simple"",
""schema"": {
""type"": ""string"",
""nullable"": true
}
}".ChangeLineBreaks(), json2);
}

[Fact]
public void CreateOrderByAndSelectAndExpandParametersWorks()
{
Expand Down