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

Add testcases for allOf cases #2769

Merged
merged 2 commits into from
Oct 2, 2023
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
10 changes: 6 additions & 4 deletions src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static IEnumerable<string> GetSchemaNames(this OpenApiSchema schema)
}
internal static IEnumerable<OpenApiSchema> FlattenSchemaIfRequired(this IList<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter)
{
return schemas.Count == 1 && string.IsNullOrEmpty(schemas[0].Title) ?
if (schemas is null) return Enumerable.Empty<OpenApiSchema>();
return schemas.Count == 1 ?
schemas.FlattenEmptyEntries(subsequentGetter, 1) :
schemas;
}
Expand Down Expand Up @@ -74,8 +75,9 @@ public static bool IsInclusiveUnion(this OpenApiSchema? schema)

public static bool IsInherited(this OpenApiSchema? schema)
{
var meaningfulSchemas = schema?.AllOf?.Where(IsSemanticallyMeaningful);
return meaningfulSchemas?.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) == 1 && meaningfulSchemas.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) == 1;
if (schema is null) return false;
var meaningfulSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf).Where(IsSemanticallyMeaningful).ToArray();
return meaningfulSchemas.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) == 1 && meaningfulSchemas.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) == 1;
}

internal static OpenApiSchema? MergeIntersectionSchemaEntries(this OpenApiSchema? schema)
Expand All @@ -99,7 +101,7 @@ public static bool IsExclusiveUnion(this OpenApiSchema? schema)
{
return schema?.OneOf?.Count(IsSemanticallyMeaningful) > 1;
}
private static readonly HashSet<string> oDataTypes = new() {
private static readonly HashSet<string> oDataTypes = new(StringComparer.OrdinalIgnoreCase) {
"number",
"integer",
};
Expand Down
113 changes: 113 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6969,4 +6969,117 @@ public async Task ComplexInheritanceStructures()
Assert.NotNull(classificationPrimerClass);
Assert.Single(classificationPrimerClass.Properties.Where(static x => x.Name.Equals("name", StringComparison.OrdinalIgnoreCase)));
}
[Fact]
public async Task InheritanceWithAllOfInBaseType()
{
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
await using var fs = await GetDocumentStream(@"openapi: 3.0.1
info:
title: OData Service for namespace microsoft.graph
description: This OData service is located at https://graph.microsoft.com/v1.0
version: 1.0.1
servers:
- url: https://graph.microsoft.com/v1.0
paths:
/directoryObject:
get:
responses:
'200':
description: Example response
content:
application/json:
schema:
$ref: '#/components/schemas/microsoft.graph.directoryObject'
components:
schemas:
microsoft.graph.directoryObject:
type: object
allOf:
- title: 'directoryObject'
required: ['@odata.type']
type: 'object'
properties:
'@odata.type':
type: 'string'
default: '#microsoft.graph.directoryObject'
discriminator:
propertyName: '@odata.type'
mapping:
'#microsoft.graph.user': '#/components/schemas/microsoft.graph.user'
'#microsoft.graph.group': '#/components/schemas/microsoft.graph.group'
microsoft.graph.group:
allOf:
- '$ref': '#/components/schemas/microsoft.graph.directoryObject'
- title: 'group'
type: 'object'
properties:
groupprop:
type: 'string'");
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>("Group"));
}
[Fact]
public async Task InheritanceWithAllOfWith3Parts()
{
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
await using var fs = await GetDocumentStream(@"openapi: 3.0.1
info:
title: OData Service for namespace microsoft.graph
description: This OData service is located at https://graph.microsoft.com/v1.0
version: 1.0.1
servers:
- url: https://graph.microsoft.com/v1.0
paths:
/directoryObject:
get:
responses:
'200':
description: Example response
content:
application/json:
schema:
$ref: '#/components/schemas/microsoft.graph.directoryObject'
components:
schemas:
microsoft.graph.directoryObject:
title: 'directoryObject'
required: ['@odata.type']
type: 'object'
properties:
'@odata.type':
type: 'string'
default: '#microsoft.graph.directoryObject'
discriminator:
propertyName: '@odata.type'
mapping:
'#microsoft.graph.user': '#/components/schemas/microsoft.graph.user'
'#microsoft.graph.group': '#/components/schemas/microsoft.graph.group'
microsoft.graph.group:
allOf:
- '$ref': '#/components/schemas/microsoft.graph.directoryObject'
- title: 'group part 1'
type: 'object'
properties:
groupprop1:
type: 'string'
- title: 'group part 2'
type: 'object'
properties:
groupprop2:
type: 'string'");
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);
var resultClass = codeModel.FindChildByName<CodeClass>("Group");
Assert.NotNull(resultClass);
Assert.Equal(2, resultClass.Properties.Count());
Assert.Single(resultClass.Properties.Where(x => x.Name.Equals("groupprop1", StringComparison.OrdinalIgnoreCase)));
Assert.Single(resultClass.Properties.Where(x => x.Name.Equals("groupprop2", StringComparison.OrdinalIgnoreCase)));
}
}
Loading