OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG] [ASPNETCORE] nullable array does not generate a nullable property #19516

Open chrisburkh opened 2 months ago

chrisburkh commented 2 months ago

Bug Report Checklist

openapi-generator version

7.8.0

Description

When an array is makred as nullable in the openapi spec the generated property is NOT nullable

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
servers: []
paths: {}
components:
  schemas:
    Pet:
      type: object
      properties:
        roleTypes:
          type: array
          items:
            type: string
          nullable: true

Generated Code:

        [DataMember(Name="roleTypes", EmitDefaultValue=true)]
        public List<string> RoleTypes { get; set; }

expected outcome:

        [DataMember(Name="roleTypes", EmitDefaultValue=true)]
        public List<string>? RoleTypes { get; set; }

Generation Details

openapi-generator-cli generate -i src/petstore.yaml -g aspnetcore --additional-properties nullableReferenceTypes=true,aspnetCoreVersion=8.0
GeorgeFkd commented 1 month ago

For version 3.0 it should be okk, as I see in the template it has this code for the properties. (In versions 2.0 and 2.1 this is indeed the case). I am debugging it further to see where the problem lies, as i also get the same problem when doing so.

public {{{dataType}}}{{#nullableReferenceTypes}}{{^isContainer}}{{^required}}{{^isNullable}}?{{/isNullable}}{{/required}}{{/isContainer}}{{/nullableReferenceTypes}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}

The file of interest is aspnetcore-3.0-model.mustache

GeorgeFkd commented 1 month ago

As indicated above for container objects there cannot be a nullable reference. If this is not a desired behaviour i can submit a PR with the fix. Changing the {{^isContainer}} to {{#isContainer}} and the {{^isNullable}} to {{/isNullable}} does the trick if you need a quick fix.