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

Adds mapping object to discriminator objects #170

Merged
merged 13 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -397,20 +397,25 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
}
else
{
var structuredTypeName = (structuredType as IEdmSchemaElement)?.Name;
// The discriminator object is added to structured types which have derived types.
OpenApiDiscriminator discriminator = null;
if (context.Settings.EnableDiscriminatorValue && derivedTypes.Any() && structuredType.BaseType != null)
{
discriminator = new OpenApiDiscriminator
{
PropertyName = "@odata.type"
PropertyName = "@odata.type",
Mapping = new Dictionary<string, string>
{
{structuredTypeName, "#" + structuredType.FullTypeName()}
baywet marked this conversation as resolved.
Show resolved Hide resolved
}
};
}

// A structured type without a base type is represented as a Schema Object of type object
OpenApiSchema schema = new OpenApiSchema
{
Title = (structuredType as IEdmSchemaElement)?.Name,
Title = structuredTypeName,

Type = "object",

Expand All @@ -425,7 +430,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
OneOf = null,
AnyOf = null
};

irvinesunday marked this conversation as resolved.
Show resolved Hide resolved
// It optionally can contain the field description,
// whose value is the value of the unqualified annotation Core.Description of the structured type.
if (structuredType.TypeKind == EdmTypeKind.Complex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public void FindOperationsReturnsCorrectCollectionOrOperations()
var operations = provider.FindOperations(entitySet.EntityType(), false);

// Assert
Assert.Equal(15, operations.Count());
Assert.Equal(29, operations.Count());

// Act
entitySet = model.EntityContainer.FindEntitySet("directoryObjects");

operations = provider.FindOperations(entitySet.EntityType(), false);

// Assert
Assert.Equal(30, operations.Count());
Assert.Equal(57, operations.Count());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void GetPathsForGraphBetaModelReturnsAllPaths()

// Assert
Assert.NotNull(paths);
Assert.Equal(50085, paths.Count());
Assert.Equal(29188, paths.Count());
}

[Fact]
Expand All @@ -67,7 +67,7 @@ public void GetPathsForGraphBetaModelWithDerivedTypesConstraintReturnsAllPaths()

// Assert
Assert.NotNull(paths);
Assert.Equal(50070, paths.Count());
Assert.Equal(29154, paths.Count());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,52 @@ public void CreateStructuredTypeSchemaThrowArgumentNullEnumType()
Assert.Throws<ArgumentNullException>("structuredType", () => context.CreateStructuredTypeSchema(structuredType: null));
}

[Fact]
public void CreateStructuredTypeSchemaWithDiscriminatorValueEnabledReturnsCorrectSchema()
{
// Arrange
IEdmModel model = EdmModelHelper.GraphBetaModel;
ODataContext context = new(model, new OpenApiConvertSettings
{
EnableDiscriminatorValue = true,
});

IEdmEntityType entity = model.SchemaElements.OfType<IEdmEntityType>().First(t => t.Name == "directoryObject");
Assert.NotNull(entity); // Guard

// Act
var schema = context.CreateStructuredTypeSchema(entity);
string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);

// Assert
Assert.NotNull(json);
Assert.Equal(@"{
""allOf"": [
{
""$ref"": ""#/components/schemas/microsoft.graph.entity""
},
{
""title"": ""directoryObject"",
""type"": ""object"",
""properties"": {
""deletedDateTime"": {
""pattern"": ""^[0-9]{4,}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]{1,12})?(Z|[+-][0-9][0-9]:[0-9][0-9])$"",
""type"": ""string"",
""format"": ""date-time"",
""nullable"": true
}
},
""discriminator"": {
""propertyName"": ""@odata.type"",
""mapping"": {
""directoryObject"": ""#microsoft.graph.directoryObject""
baywet marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
]
}".ChangeLineBreaks(), json);
}

[Fact]
public void CreateComplexTypeWithoutBaseSchemaReturnCorrectSchema()
{
Expand Down
Loading