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

Fix schema generation with allOf inheritance #2815

Merged
merged 1 commit into from
Apr 19, 2024
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 @@ -362,6 +362,7 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
AdditionalPropertiesAllowed = false
};

OpenApiSchema root = schema;
var applicableDataProperties = dataContract.ObjectProperties;

if (_generatorOptions.UseAllOfForInheritance || _generatorOptions.UseOneOfForPolymorphism)
Expand All @@ -370,7 +371,15 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
{
var baseTypeSchema = GenerateConcreteSchema(baseTypeDataContract, schemaRepository);

schema.AllOf.Add(baseTypeSchema);
if (_generatorOptions.UseAllOfForInheritance)
{
root = new OpenApiSchema();
root.AllOf.Add(baseTypeSchema);
}
else
{
schema.AllOf.Add(baseTypeSchema);
}

applicableDataProperties = applicableDataProperties
.Where(dataProperty => dataProperty.MemberInfo.DeclaringType == dataContract.UnderlyingType);
Expand Down Expand Up @@ -423,7 +432,12 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
schema.AdditionalProperties = GenerateSchema(dataContract.ObjectExtensionDataType, schemaRepository);
}

return schema;
if (root != schema)
{
root.AllOf.Add(schema);
}

return root;
}

private bool IsKnownSubType(DataContract dataContract, out DataContract baseTypeDataContract)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,15 @@ public void GenerateSchema_SupportsOption_UseAllOfForInheritance()
var referenceSchema = subject.GenerateSchema(typeof(SubType1), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.Equal("object", schema.Type);
Assert.Equal(new[] { "Property1" }, schema.Properties.Keys);
Assert.NotNull(schema.AllOf);
var allOf = Assert.Single(schema.AllOf);
Assert.NotNull(allOf.Reference);
Assert.Equal("BaseType", allOf.Reference.Id);
Assert.Equal(2, schema.AllOf.Count);
var baseSchema = schema.AllOf[0];
Assert.Equal("BaseType", baseSchema.Reference.Id);
Assert.NotNull(baseSchema.Reference);
var subSchema = schema.AllOf[1];
Assert.Equal(new[] { "Property1" }, subSchema.Properties.Keys);
// The base type schema
var baseTypeSchema = schemaRepository.Schemas[allOf.Reference.Id];
var baseTypeSchema = schemaRepository.Schemas[baseSchema.Reference.Id];
Assert.Equal("object", baseTypeSchema.Type);
Assert.Equal(new[] { "BaseProperty" }, baseTypeSchema.Properties.Keys);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,15 @@ public void GenerateSchema_SupportsOption_UseAllOfForInheritance()
var referenceSchema = subject.GenerateSchema(typeof(SubType1), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.Equal("object", schema.Type);
Assert.Equal(new[] { "Property1" }, schema.Properties.Keys);
Assert.NotNull(schema.AllOf);
var allOf = Assert.Single(schema.AllOf);
Assert.NotNull(allOf.Reference);
Assert.Equal("BaseType", allOf.Reference.Id);
Assert.Equal(2, schema.AllOf.Count);
var baseSchema = schema.AllOf[0];
Assert.Equal("BaseType", baseSchema.Reference.Id);
Assert.NotNull(baseSchema.Reference);
var subSchema = schema.AllOf[1];
Assert.Equal(new[] { "Property1" }, subSchema.Properties.Keys);
// The base type schema
var baseTypeSchema = schemaRepository.Schemas[allOf.Reference.Id];
var baseTypeSchema = schemaRepository.Schemas[baseSchema.Reference.Id];
Assert.Equal("object", baseTypeSchema.Type);
Assert.Equal(new[] { "BaseProperty" }, baseTypeSchema.Properties.Keys);
}
Expand Down