openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][C#/aspnetcore] Invalid C# code generated with version 7.4.0

Open JonasPed opened this issue 1 year ago • 6 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The latest release (v7.4.0) can generate C# code that does not compile when generating for aspnetcore and setting nullableReferenceTypes=true.

It happens when a property is optional and declared with "nullable: true". See the problem property in the example OpenAPI document. In the model class it generates the property where the type is declared with double question marks (??) instead of a single question mark.

Using version v7.3.0 it works fine.

openapi-generator version

7.4.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: A bug report
  description: Example for bug report
  version: "1.0.0"
tags:
  - name: demo
    description: Demo tag

paths:
  /v1/example:
    get:
      tags:
        - demo
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/demoResponse'
                
components:
  schemas:
    demoResponse:
      type: object
      required:
        - ok
      properties:
        problem:
          type: string
          nullable: true
          description: This generates code that can not compile. 
        ok:
          type: string
          nullable: true
          description: This genrates code that can compile.

Generation Details

Using docker image: docker run --rm --user $(id -u) -v $(pwd)/Documentation:/local openapitools/openapi-generator-cli:latest-release generate -i /local/api.yaml -o /local/Generated -g aspnetcore -p nullableReferenceTypes=true

or cli java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ~/api.yaml -o ./output -g aspnetcore -p nullableReferenceTypes=true

Steps to reproduce
  1. Run below command to generate code from OpenAPI document. java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ~/api.yaml -o ./output -g aspnetcore -p nullableReferenceTypes=true
  2. Run below command to build dotnet solution. dotnet build output/
Related issues/PRs
Suggest a fix

JonasPed avatar Mar 12 '24 08:03 JonasPed

thanks for reporting the issue.

I think it's caused by https://github.com/OpenAPITools/openapi-generator/pull/17934

@JFCote can you please take a look? shall we revert the change for the time being?

wing328 avatar Mar 13 '24 07:03 wing328

Hi @wing328 , I already declared the bug a couple of days after my changes but didn't had the time to fix it before the release: https://github.com/OpenAPITools/openapi-generator/issues/18005

I think we can safely revert the change and I'll try to do a safer change next time. In the end, it needs to "accumulate" all the reason to be nullable and only render one ? and with the way the code is working, it needs a little bit of work.

Do you take care of the revert or do you want me to do it @wing328 ?

JFCote avatar Mar 13 '24 10:03 JFCote

There is OptionalParameterLambda which can mitigate this.

devhl-labs avatar Mar 15 '24 22:03 devhl-labs

We are having the same issues with the latest versions and a C# 6 / ASP.NET Core 6 codebase. We forced our build process to v7.3.0 for now.

mpo-wd avatar Jun 25 '24 06:06 mpo-wd

@mpo-wd if you or anyone would like to contribute a PR or sponsor the fix, please let us know.

wing328 avatar Jun 25 '24 09:06 wing328

@wing328 I will definitely try to do a fix in the upcoming weeks but if someone can chime in before that, I will do the review. Sorry about that, been a crazy year!

JFCote avatar Jun 25 '24 11:06 JFCote

@wing328 I created a PR to fix this issue here: https://github.com/OpenAPITools/openapi-generator/pull/19062

JFCote avatar Jul 03 '24 13:07 JFCote

PR merged. thanks for the fix

wing328 avatar Jul 06 '24 13:07 wing328