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

Fixes inheritance evaluation when a referenced schema in an allOf #5886

Merged
merged 3 commits into from
Dec 9, 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
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
Loading