Skip to content
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

Temporarily remove support for alternate keys to allow for further discussions #299

Merged
merged 1 commit into from
Oct 24, 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 @@ -212,46 +212,6 @@ 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,7 +15,7 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PackageId>Microsoft.OpenApi.OData</PackageId>
<SignAssembly>true</SignAssembly>
<Version>1.2.0-preview4</Version>
<Version>1.2.0-preview5</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>
Expand All @@ -24,7 +24,6 @@
- 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
- Set assembly version used for conversion in OpenApiInfo object #208
- Add support for alternate keys parameters #120
- Adds create and update operations to non-containment navigation properties when restrictions annotations are explicitly set to true #265
- Generate odata.nextLink and odata.deltaLink properties on delta functions response schemas #285
- Omits paths with PathItems without any operation #148
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,95 +279,6 @@ 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