OpenAPITools / OpenAPITools/openapi-generator

[BUG] [aspnetcore] Nullable annotations work incorrectly with nested objects

Open
#11,028 1 comment 5 reactions 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 (example)?
  • 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

The generator produces invalid source code (missing nullable type annotation ? ) for models that contain nullable nested objects

openapi-generator version

openapi-generator-cli version : 5.3.0

OpenAPI declaration file content or url
openapi: 3.0.1

info:
  title: test-api
  version: 1.0.0

paths:
  "/":
    get:
      operationId: get
      summary: Get test data
      responses:
        200:
          description: Test data
          content:
            application/json:
              schema:
                type: object
                title: Data
                properties:
                  subObject:
                    type: object
                    nullable: true
                    properties:
                      name:
                        type: string
                  name:
                    type: string
                    nullable: true
Generation Details

The following command is used to generate source code:

java -jar openapi-generator-cli.jar generate -g aspnetcore --model-name-suffix Model -c aspnetcore-config.json -i spec.yaml -o generated

and aspnetcore-config.json contains the following data:

{
    "aspnetCoreVersion": "5.0",
    "nullableReferenceTypes": true,
    "packageName": "Test"
}
Steps to reproduce
  1. Create a specification file and name it spec.yaml
  2. Create a configuration file and name it apsnetcore-config.json
  3. Run the command (see generation details)
  4. Make sure that generated/src/Test/Models/DataModel.cs contains an incorrect model:
/// <summary>
/// Gets or Sets SubObject
/// </summary>
[DataMember(Name="subObject", EmitDefaultValue=true)]
public DataSubObjectModel SubObject { get; set; }

/// <summary>
/// Gets or Sets Name
/// </summary>
[DataMember(Name="name", EmitDefaultValue=true)]
public string? Name { get; set; }

but expected (missing ? after DataSubObjectModel):

/// <summary>
/// Gets or Sets SubObject
/// </summary>
[DataMember(Name="subObject", EmitDefaultValue=true)]
public DataSubObjectModel? SubObject { get; set; }

/// <summary>
/// Gets or Sets Name
/// </summary>
[DataMember(Name="name", EmitDefaultValue=true)]
public string? Name { get; set; }
Related issues/PRs

There is a merged PR that adds support for nullable reference types: https://github.com/OpenAPITools/openapi-generator/pull/9620

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 with spec.yaml, aspnetcore-config.json, and the documented Java CLI command, then inspect generated/src/Test/Models/DataModel.cs. Compare the generated nested SubObject property with the expected C# output and verify that nullable nested objects receive the ? annotation without changing the already-correct Name property.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.