Skip to content

Commit

Permalink
Adds mapping object to discriminator objects (#170)
Browse files Browse the repository at this point in the history
* Add native JSON discriminator property

* Add OpenAPI v3.0.X native JSON discriminator support

* Add setting for enabling discriminator support for OpenAPI V3.0.X

* Update integration file

* Remove ConvertSettings property

* Add mapping object to discriminator object; add validation test

* Fix breaking tests due to update of beta CSDL file

* Fix break test

* Use the structured type full type name and V3 reference identifier for mapping

* Update src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs

Co-authored-by: Vincent Biret <[email protected]>

* Add back whitespace for code readability

* Add # to mapping key

Co-authored-by: Vincent Biret <[email protected]>
  • Loading branch information
irvinesunday and baywet authored Feb 8, 2022
1 parent 9183c9a commit 820c68e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,22 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
OpenApiDiscriminator discriminator = null;
if (context.Settings.EnableDiscriminatorValue && derivedTypes.Any() && structuredType.BaseType != null)
{
string v3RefIdentifier = new OpenApiSchema
{
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = structuredType.FullTypeName()
}
}.Reference.ReferenceV3;

discriminator = new OpenApiDiscriminator
{
PropertyName = "@odata.type"
PropertyName = "@odata.type",
Mapping = new Dictionary<string, string>
{
{"#" + structuredType.FullTypeName(), v3RefIdentifier }
}
};
}

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"": {
""#microsoft.graph.directoryObject"": ""#/components/schemas/microsoft.graph.directoryObject""
}
}
}
]
}".ChangeLineBreaks(), json);
}

[Fact]
public void CreateComplexTypeWithoutBaseSchemaReturnCorrectSchema()
{
Expand Down

0 comments on commit 820c68e

Please sign in to comment.