Skip to content

Commit

Permalink
Merge pull request #2782 from papegaaij/broken-inheritance
Browse files Browse the repository at this point in the history
Add testcase demonstrating broken inhertance chain
  • Loading branch information
baywet authored Sep 29, 2023
2 parents 5a370c4 + 3081814 commit 75499c8
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Updated constructor for request builders in Python to set passed path parameters. [#3352](https://github.com/microsoft/kiota/issues/3352)
- Fixed inherited type definition generation where some cases would not generate properly. [#2745](https://github.com/microsoft/kiota/issues/2745)
- Fixed naming convention for inline response types. [#2952](https://github.com/microsoft/kiota/issues/2952)
- Localhost based descriptions are not cached anymore to facilitate development workflows. [#3316](https://github.com/microsoft/kiota/issues/3316)
- Fixed a bug where the hints would miss quotes for paths and always use the API manifest. [#3342](https://github.com/microsoft/kiota/issues/3342)
Expand Down
13 changes: 8 additions & 5 deletions src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public static IEnumerable<string> GetSchemaNames(this OpenApiSchema schema)
return new[] { schema.Xml.Name };
return Enumerable.Empty<string>();
}
private static IEnumerable<string> FlattenIfRequired(this IList<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter)
internal static IEnumerable<OpenApiSchema> FlattenSchemaIfRequired(this IList<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter)
{
return (schemas.Count == 1 && string.IsNullOrEmpty(schemas[0].Title) ?
return schemas.Count == 1 && string.IsNullOrEmpty(schemas[0].Title) ?
schemas.FlattenEmptyEntries(subsequentGetter, 1) :
schemas)
.Select(static x => x.Title).Where(static x => !string.IsNullOrEmpty(x));
schemas;
}
private static IEnumerable<string> FlattenIfRequired(this IList<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter)
{
return schemas.FlattenSchemaIfRequired(subsequentGetter).Where(static x => !string.IsNullOrEmpty(x.Title)).Select(static x => x.Title);
}

public static string GetSchemaName(this OpenApiSchema schema)
Expand Down Expand Up @@ -154,7 +157,7 @@ public static IEnumerable<string> GetSchemaReferenceIds(this OpenApiSchema schem

return Enumerable.Empty<string>();
}
internal static IEnumerable<OpenApiSchema> FlattenEmptyEntries(this IEnumerable<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter, int? maxDepth = default)
private static IEnumerable<OpenApiSchema> FlattenEmptyEntries(this IEnumerable<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter, int? maxDepth = default)
{
if (schemas == null) return Enumerable.Empty<OpenApiSchema>();
ArgumentNullException.ThrowIfNull(subsequentGetter);
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ private CodeType CreateModelDeclarationAndType(OpenApiUrlTreeNode currentNode, O
}
private CodeTypeBase CreateInheritedModelDeclaration(OpenApiUrlTreeNode currentNode, OpenApiSchema schema, OpenApiOperation? operation, string classNameSuffix, CodeNamespace codeNamespace, bool isRequestBody)
{
var allOfs = schema.AllOf.FlattenEmptyEntries(static x => x.AllOf);
var allOfs = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf);
CodeElement? codeDeclaration = null;
var className = string.Empty;
var codeNamespaceFromParent = GetShortestNamespace(codeNamespace, schema);
Expand Down
103 changes: 103 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6866,4 +6866,107 @@ public async Task SupportsMultiPartFormAsRequestBody()
var addressClass = codeModel.FindChildByName<CodeClass>("Address");
Assert.NotNull(addressClass);
}
[Fact]
public async Task ComplexInheritanceStructures()
{
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
await using var fs = await GetDocumentStream(@"openapi: 3.0.1
info:
title: Broken inheritance
version: '1'
servers:
- url: http://localhost
paths:
'/groupclassification':
get:
summary: Example
description: Example
responses:
'200':
description: default response
content:
application/vnd.topicus.keyhub+json;version=latest:
schema:
'$ref': '#/components/schemas/group.GroupClassification'
components:
schemas:
Linkable:
required:
- '$type'
type: object
properties:
'$type':
type: string
discriminator:
propertyName: '$type'
mapping:
group.GroupPrimer: '#/components/schemas/group.GroupPrimer'
group.GroupClassificationPrimer: '#/components/schemas/group.GroupClassificationPrimer'
group.GroupClassification: '#/components/schemas/group.GroupClassification'
group.GroupPrimer:
allOf:
- '$ref': '#/components/schemas/Linkable'
- type: object
properties:
markers:
'$ref': '#/components/schemas/mark.ItemMarkers'
NonLinkable:
required:
- '$type'
type: object
properties:
'$type':
type: string
discriminator:
propertyName: '$type'
mapping:
mark.ItemMarkers: '#/components/schemas/mark.ItemMarkers'
group.GroupsAuditStats: '#/components/schemas/group.GroupsAuditStats'
mark.ItemMarkers:
allOf:
- '$ref': '#/components/schemas/NonLinkable'
- type: object
group.GroupClassificationPrimer:
allOf:
- '$ref': '#/components/schemas/Linkable'
- required:
- '$type'
- name
type: object
properties:
'$type':
type: string
name:
type: string
discriminator:
propertyName: '$type'
mapping:
group.GroupClassification: '#/components/schemas/group.GroupClassification'
group.GroupClassification:
allOf:
- '$ref': '#/components/schemas/group.GroupClassificationPrimer'
- type: object
properties:
description:
type: string
group.GroupsAuditStats:
allOf:
- '$ref': '#/components/schemas/NonLinkable'
- type: object
properties:
classification:
'$ref': '#/components/schemas/group.GroupClassification'");
var mockLogger = new Mock<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration { ClientClassName = "Graph", OpenAPIFilePath = tempFilePath }, _httpClient);
var document = await builder.CreateOpenApiDocumentAsync(fs);
var node = builder.CreateUriSpace(document);
var codeModel = builder.CreateSourceModel(node);
Assert.NotNull(codeModel.FindChildByName<CodeClass>("Linkable"));
var classificationClass = codeModel.FindChildByName<CodeClass>("GroupClassification");
Assert.Single(classificationClass.Properties.Where(static x => x.Name.Equals("description", StringComparison.OrdinalIgnoreCase)));
Assert.NotNull(classificationClass);
var classificationPrimerClass = codeModel.FindChildByName<CodeClass>("GroupClassificationPrimer");
Assert.NotNull(classificationPrimerClass);
Assert.Single(classificationPrimerClass.Properties.Where(static x => x.Name.Equals("name", StringComparison.OrdinalIgnoreCase)));
}
}

0 comments on commit 75499c8

Please sign in to comment.