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

SDK Generation does not handle endpoints with hyphen (-) characters #3599

Closed
barrettnichting opened this issue Oct 31, 2023 · 5 comments · Fixed by #3628
Closed

SDK Generation does not handle endpoints with hyphen (-) characters #3599

barrettnichting opened this issue Oct 31, 2023 · 5 comments · Fixed by #3628
Assignees
Labels
generator Issues or improvements relater to generation capabilities. type:bug A broken experience WIP
Milestone

Comments

@barrettnichting
Copy link

When generating an SDK on an API that contains hyphenated endpoints namespace values are generated with hyphen characters and will not build.

path: /api/v1/my-api

This generates a namespace such as ApiSDK.Models.My-api that is invalid and will not compile.

@github-project-automation github-project-automation bot moved this to Todo in Kiota Oct 31, 2023
@baywet baywet self-assigned this Oct 31, 2023
@baywet baywet added question generator Issues or improvements relater to generation capabilities. labels Oct 31, 2023
@baywet baywet added this to the Backlog milestone Oct 31, 2023
@baywet
Copy link
Member

baywet commented Oct 31, 2023

Hi @barrettnichting
Thanks for using kiota and for reaching out.
Which version of Kiota are you using?
Which language are you generating clients in?
What value did you pass to the -n | --client-namespace-name parameter?
Can you provide an OpenAPI description that reproduces the issue please?

The following unit test pases.

[Fact]
    public async Task HandlesSpecialCharactersInPathSegment()
    {
        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:
  /my-api:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                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);
        Assert.Null(codeModel.FindNamespaceByName("ApiSdk.my-api"));
        Assert.NotNull(codeModel.FindNamespaceByName("ApiSdk.MyApi"));
    }

@barrettnichting
Copy link
Author

barrettnichting commented Oct 31, 2023

Hi @baywet
Thank you for the quick reply.
I am using the VSCode extension v1.7.100000001 Preview
Note: Same issue on v1.8.23102601 Pre-Release
I cannot provide the OpenApi description as it is for a private Api. I can possibly create a sample yaml and project if the below information is not enough.

I am importing my yaml and selecting a single path (deeply nested) that has a hyphen then using "generate api client"
class = ApiClient
class namespace = ApiSDK
prjoect name = TestProject
Generate CSharp

Then I am creating a dotnet 7.0 console app.
dotnet new console --framework net7.0

I attempt to run this and get a build error.
dotnet run

Various build errors such as:
C:\temp\Kiota-Testing\Test1\TestProject\Models\Change-events\ChangeEvent.cs(7,32): error CS0116: A namespace cannot directly contain members such as fields, methods or statements [C:\temp\Kiota-Testing\Test1\Test1.csproj]

The namespace in my generated model has the hyphen and I believe is the problem.
namespace ApiSDK.Models.Change-events {

@baywet
Copy link
Member

baywet commented Oct 31, 2023

Thanks for the additional details.
So the hyphen is not in the API path segments but in the schema components names in the description?

@barrettnichting
Copy link
Author

@baywet That is correct it is in both, but it looks like the schema is actually the problem.

Snippet:

schemas:
    Change-events.changeEvent:
          required:
          - id

@baywet
Copy link
Member

baywet commented Nov 1, 2023

Thanks for the additional information, I put together #3628 to address this issue.

@github-project-automation github-project-automation bot moved this from In Progress to Done in Kiota Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generator Issues or improvements relater to generation capabilities. type:bug A broken experience WIP
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants