[BUG][C#/aspnetcore] Invalid C# code generated with version 7.4.0
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
- 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 - Run below command to build dotnet solution.
dotnet build output/
Related issues/PRs
Suggest a fix
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?
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 ?
There is OptionalParameterLambda which can mitigate this.
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 if you or anyone would like to contribute a PR or sponsor the fix, please let us know.
@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!
@wing328 I created a PR to fix this issue here: https://github.com/OpenAPITools/openapi-generator/pull/19062
PR merged. thanks for the fix