Skip to content

Commit

Permalink
Merge pull request #5886 from microsoft/andrueastman/fixMeaninigFulIn…
Browse files Browse the repository at this point in the history
…heritance

Fixes inheritance evaluation when a referenced schema in an allOf
  • Loading branch information
andrueastman authored Dec 9, 2024
2 parents 801826a + 8d56584 commit e8fb806
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed a bug in generation when a referenced schema in an allOf was a primitive [#5701](https://github.com/microsoft/kiota/issues/5701).

## [1.21.0] - 2024-12-05

### Added
Expand Down
6 changes: 5 additions & 1 deletion src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ public static bool IsInclusiveUnion(this OpenApiSchema? schema, uint exclusiveMi
public static bool IsInherited(this OpenApiSchema? schema)
{
if (schema is null) return false;
var meaningfulMemberSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf).Where(static x => x.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true)).ToArray();
var meaningfulMemberSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf)
.Where(static x => x.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true))
// the next line ensures the meaningful schema are objects as it won't make sense inheriting from a primitive despite it being meaningful.
.Where(static x => string.IsNullOrEmpty(x.Reference?.Id) || string.IsNullOrEmpty(x.Type) || "object".Equals(x.Type, StringComparison.OrdinalIgnoreCase))
.ToArray();
var isRootSchemaMeaningful = schema.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true);
return meaningfulMemberSchemas.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) == 1 &&
(meaningfulMemberSchemas.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) == 1 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,32 @@ public void IsIntersection()
};
Assert.False(schema.IsInherited());
Assert.False(schema.IsIntersection());

schema = new OpenApiSchema
{
Title = "Trader Id",
AllOf = new List<OpenApiSchema> {
new ()
{
Title = "UserId",
Description = "unique identifier",
Type = "string",
Pattern = "^[1-9][0-9]*$",
Example = new OpenApiString("1323232"),
Reference = new OpenApiReference
{
Id = "UserId" // This property makes the schema "meaningful"
}
}
},
Reference = new OpenApiReference
{
Id = "TraderId" // This property makes the schema "meaningful"
}
};

Assert.False(schema.IsInherited());
Assert.False(schema.IsIntersection());
}
[Fact]
public void MergesIntersection()
Expand Down

0 comments on commit e8fb806

Please sign in to comment.