Skip to content

Commit

Permalink
fix: adds unit test for nullable intersection types
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Sep 5, 2024
1 parent 3216b5c commit 678da53
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2840,6 +2840,89 @@ public void SquishesLonelyNullablesBothAnyOf()
Assert.True(executorMethod.ReturnType is CodeType); // not union
Assert.Null(codeModel.FindChildByName<CodeClass>("createUploadSessionResponseMember1"));
}
[Fact]
public void SupportsNullableAnyOf()
{
var anyOfSchema = new OpenApiSchema
{
Type = "object",
AdditionalPropertiesAllowed = false,
Properties = new Dictionary<string, OpenApiSchema> {
{
"date", new OpenApiSchema {
AnyOf = [
new OpenApiSchema {
Type = "string",
Nullable = true
},
new OpenApiSchema {
Type = "number",
Format = "int64",
Nullable = true,
}
]
}
}
},
Reference = new OpenApiReference
{
Id = "anyOfNullable",
Type = ReferenceType.Schema
},
UnresolvedReference = false
};
var document = new OpenApiDocument
{
Paths = new OpenApiPaths
{
["createUploadSession"] = new OpenApiPathItem
{
Operations = {
[OperationType.Get] = new OpenApiOperation
{
Responses = new OpenApiResponses
{
["200"] = new OpenApiResponse
{
Content = new Dictionary<string, OpenApiMediaType> {
["application/json"] = new OpenApiMediaType
{
Schema = anyOfSchema
}
}
}
}
}
}
}
},
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, OpenApiSchema> {
{
"anyOfNullable", anyOfSchema
}
},
},
};
var mockLogger = new Mock<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration { ClientClassName = "Graph", ApiRootUrl = "https://localhost" }, _httpClient);
builder.SetOpenApiDocument(document);
var node = builder.CreateUriSpace(document);
var codeModel = builder.CreateSourceModel(node);
var anyOfClass = codeModel.FindChildByName<CodeClass>("anyOfNullable");
Assert.NotNull(anyOfClass);
var dateProperty = anyOfClass.FindChildByName<CodeProperty>("date", false);
Assert.NotNull(dateProperty);
if (dateProperty.Type is not CodeIntersectionType unionType)
Assert.Fail("Date property type is not a union type");
else
{
Assert.Equal(2, unionType.Types.Count());
Assert.Contains(unionType.Types, x => x.Name.Equals("string", StringComparison.OrdinalIgnoreCase));
Assert.Contains(unionType.Types, x => x.Name.Equals("int64", StringComparison.OrdinalIgnoreCase));
}
}

[Fact]
public void AddsDiscriminatorMappings()
Expand Down

0 comments on commit 678da53

Please sign in to comment.