Skip to content

Commit

Permalink
Fix schema generation with allOf inheritance (#2815)
Browse files Browse the repository at this point in the history
Resolves #2268.
  • Loading branch information
bkoelman authored Apr 19, 2024
1 parent 1c29b4b commit a17b464
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
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

0 comments on commit a17b464

Please sign in to comment.