Skip to content

Commit

Permalink
Merge pull request #5509 from microsoft/andrueastman/typeMapping
Browse files Browse the repository at this point in the history
Fixes incorrect type mapping
  • Loading branch information
andrueastman authored Oct 1, 2024
2 parents 9f628e7 + 8580374 commit 501cca2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated schema link in plugin manifest to the correct url. [#5441](https://github.com/microsoft/kiota/issues/5441)
- Removed registration of default serialization and deserialization classes in client constructor. [#5478](https://github.com/microsoft/kiota/pull/5478)
- Fixed incorrect type name generation in aliased scenario in TS due to broad searching of types in root namespaces. [#5404](https://github.com/microsoft/kiota/issues/5404)
- Fixed incorrect type mapping in request builders with subsequent paths with the same name. [#5462](https://github.com/microsoft/kiota/issues/5462)

## [1.18.0] - 2024-09-05

Expand Down
5 changes: 4 additions & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,10 @@ x.Parent is CodeIndexer ||
Parallel.ForEach(unmappedRequestBuilderTypes, parallelOptions, x =>
{
var parentNS = x.Parent?.Parent?.Parent as CodeNamespace;
x.TypeDefinition = parentNS?.FindChildrenByName<CodeClass>(x.Name).MinBy(shortestNamespaceOrder);
CodeClass[] exceptions = x.Parent?.Parent is CodeClass parentClass ? [parentClass] : [];
x.TypeDefinition = parentNS?.FindChildrenByName<CodeClass>(x.Name)
.Except(exceptions)// the property method should not reference itself as a return type.
.MinBy(shortestNamespaceOrder);
// searching down first because most request builder properties on a request builder are just sub paths on the API
if (x.TypeDefinition == null)
{
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 @@ -231,6 +231,74 @@ await File.WriteAllTextAsync(tempFilePath, @$"openapi: 3.0.1
Assert.NotNull(specializedNS.FindChildByName<CodeClass>("StorageAccount", false));
}
[Fact]
public async Task HandlesPathWithRepeatedSegment()
{
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
await File.WriteAllTextAsync(tempFilePath, @$"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://api.funtranslations.com
paths:
/media/response/response/{{id}}:
get:
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/MediaResponseModel'
components:
schemas:
MediaResponseModel:
type: object
properties:
name:
type: string
id:
type: string
format: uuid
mediaType:
type: string
url:
type: string");
var mockLogger = new Mock<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration
{
ClientClassName = "Graph",
OpenAPIFilePath = "https://api.apis.guru/v2/specs/funtranslations.com/starwars/2.3/swagger.json"
}, _httpClient);
await using var fs = new FileStream(tempFilePath, FileMode.Open);
var document = await builder.CreateOpenApiDocumentAsync(fs);
var node = builder.CreateUriSpace(document);
builder.SetApiRootUrl();
var codeModel = builder.CreateSourceModel(node);
var rootNS = codeModel.FindNamespaceByName("ApiSdk");
Assert.NotNull(rootNS);
var responseBuilderNs = codeModel.FindNamespaceByName("ApiSdk.media.response");
Assert.NotNull(responseBuilderNs);
var responseRequestBuilder = responseBuilderNs.FindChildByName<CodeClass>("ResponseRequestBuilder", false);
Assert.NotNull(responseRequestBuilder);
var navigationProperty = responseRequestBuilder.Properties.FirstOrDefault(prop =>
prop.IsOfKind(CodePropertyKind.RequestBuilder) &&
prop.Name.Equals("Response", StringComparison.OrdinalIgnoreCase));
Assert.NotNull(navigationProperty);
var navigationPropertyType = navigationProperty.Type as CodeType;
Assert.NotNull(navigationPropertyType);
Assert.NotEqual(responseRequestBuilder, navigationPropertyType.TypeDefinition);// the request builder should not be the same as the class it is in.
var nestedResponseBuilderNs = codeModel.FindNamespaceByName("ApiSdk.media.response.response");
var nestedResponseRequestBuilder = nestedResponseBuilderNs.FindChildByName<CodeClass>("ResponseRequestBuilder", false);
Assert.Equal(nestedResponseRequestBuilder, navigationPropertyType.TypeDefinition);// the request builder should not be the same as the class it is in.
}
[Fact]
public async Task HandlesPathWithItemInNameSegment()
{
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
Expand Down

0 comments on commit 501cca2

Please sign in to comment.