OpenAPITools / OpenAPITools/openapi-generator

[BUG] [csharp] OpenAPI generator fails create nullable array items

Open
#21,281 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

I have such a component in swagger.json (openapi version: "3.0.1")

      "testNullable": {
        "type": "array",
        "items": {
          "type": "string",
          "format": "uuid",
          "nullable": true,
          "x-nullable": true
        },
        "nullable": true,
        "x-nullable": true
      }

With OpenAPI generator, I intend to create such a type in C#:
List<Guid?>?
However, as a result, it generates
List<Guid>?
I traced the models, to find the dataType as follows (datatypeWithEnum with the same value):

"dataType" : "List<Guid>", 
"datatypeWithEnum" : "List<Guid>"

and IsNullable is true
"isNullable" : true
For its items, the dataType is Guid and isNullable is true

"dataType" : "Guid",
"datatypeWithEnum" : "Guid",
"isNullable" : true,

The mustache statement is
{{{datatypeWithEnum}}}{{#lambda.first}}{{#isNullable}}{{>NullConditionalProperty}}...
So we can tell that the '?' actually comes from NullConditionalProperty.

Question: Is it supported that the array items can be nullable for C# language generator?

It seems to be specifically avoided for generichost libaray. Is there any reason? How can I work it around? @devhl-labs

Image

openapi-generator version

openapi-generator-cli-7.12.0.jar

OpenAPI declaration file content or url

https://gist.github.com/jasonxu08/40d66713fba773e3903e95140e71f29f

Generation Details

java -jar openapi-generator-cli-7.12.0.jar generate -g csharp -i "./weather.json" -o "src/w1/" -c "./config.yaml" --additional-properties="packageName=testWeather,targetFramework=net8.0,nullableReferenceTypes=true,library=generichost,validatable=false,optionalEmitDefaultValues=true"

Steps to reproduce

Save the swagger description to 'weather.json' and run the generation command.

Related issues/PRs
Suggest a fix

In this case, I suppose that the nullable mark (?) should be contained with datatypeWithEnum or any other property.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the linked OpenAPI example as weather.json, config.yaml, and the provided csharp generation command. Start by tracing how datatypeWithEnum and isNullable are handled for array items and the NullConditionalProperty mustache statement; done means the generated type represents the nullable array items as List<Guid?>?.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.