Skip to content

Commit

Permalink
Merge pull request #4606 from microsoft/andrueastman/multiDimensional…
Browse files Browse the repository at this point in the history
…Arrays

Handling of nested arrays
  • Loading branch information
andrueastman authored May 6, 2024
2 parents 448ce97 + 4f0762a commit ec1639e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
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

- Aligns naming of sliced OpenAPI description generated by `plugin add` should be named `<plugin-name>-openapi.json|yml`
- Fixed RPC server to respect the `KIOTA_CONFIG_PREVIEW` flag.
- Fixed handling of nested arrays to be handled as `UntypedNode` instances [#4549](https://github.com/microsoft/kiota/issues/4549)

## [1.14.0] - 2024-05-02

Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,8 @@ private CodeTypeBase CreateModelDeclarations(OpenApiUrlTreeNode currentNode, Ope
return CreateModelDeclarationAndType(currentNode, schema, operation, codeNamespace, suffix, response: responseValue, typeNameForInlineSchema: typeNameForInlineSchema, isRequestBody);
}

if (schema.IsArray())
if (schema.IsArray() &&
!schema.Items.IsArray()) // Only handle collections of primitives and complex types. Otherwise, multi-dimensional arrays would be recursively unwrapped undesirably to lead to incorrect serialization types.
{
// collections at root
return CreateCollectionModelDeclaration(currentNode, schema, operation, codeNamespace, typeNameForInlineSchema, isRequestBody);
Expand Down
68 changes: 68 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,74 @@ public void OData_doubles_as_any_of()
Assert.Equal("double", progressProp.Type.Name);
}
[Fact]
public void MultiNestedArraysSupportedAsUntypedNodes()
{
var fooSchema = new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema> {
{
"sortBy", new OpenApiSchema {
Type = "array",
Items = new OpenApiSchema {
Type = "array",
Items = new OpenApiSchema {
Type = "string"
}
}
}
},
},
Reference = new OpenApiReference
{
Id = "#/components/schemas/bar.foo"
},
UnresolvedReference = false
};
var document = new OpenApiDocument
{
Paths = new OpenApiPaths
{
["foos/{id}"] = new OpenApiPathItem
{
Operations = {
[OperationType.Get] = new OpenApiOperation
{
Responses = new OpenApiResponses {
["200"] = new OpenApiResponse
{
Content = {
["application/json"] = new OpenApiMediaType
{
Schema = fooSchema
}
}
}
}
}
}
},
},
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, OpenApiSchema> {
{
"bar.foo", fooSchema
}
}
}
};
var mockLogger = new CountLogger<KiotaBuilder>();
var builder = new KiotaBuilder(mockLogger, new GenerationConfiguration { ClientClassName = "Graph", ApiRootUrl = "https://localhost" }, _httpClient);
var node = builder.CreateUriSpace(document);
var codeModel = builder.CreateSourceModel(node);
var fooClass = codeModel.FindNamespaceByName("ApiSdk.models").FindChildByName<CodeClass>("foo");
Assert.NotNull(fooClass);
var sortByProp = fooClass.FindChildByName<CodeProperty>("sortBy", false);
Assert.NotNull(sortByProp);
Assert.Equal(KiotaBuilder.UntypedNodeName, sortByProp.Type.Name);// nested array property an UntypedNode
}
[Fact]
public void Object_Arrays_are_supported()
{
var userSchema = new OpenApiSchema
Expand Down

0 comments on commit ec1639e

Please sign in to comment.