Skip to content

[BUG][CSHARP] Regression with collections and nullable types #22066

@seniorquico

Description

@seniorquico
Description

I am generating a set of ASP.NET Core controller stubs and models. I upgraded to v7.16.0 from v7.15.0, and the newly generated code fails to compile with invalid nullable reference type declarations:

public Collection?<string> Items { get; set; }
//               ^
//               error CS1519: Invalid token '<' in a member declaration

The question mark is placed in between the type name and the generic type argument. It needs to be placed after the generic type argument.

openapi-generator version

The problem occurs in v7.16.0. Reverting to v7.15.0, the problem does not occur.

OpenAPI declaration file content or url

I can't share this OpenAPI specification file, but the JSON schema that produces the error is pretty straightforward:

type: object
properties:
  items:
    type: array
    items:
      type: string
Generation Details
additionalProperties:
  aspnetCoreVersion: "8.0"
  buildTarget: library
  licenseUrl: https://example.com/license
  newtonsoftVersion: "8.0.20"
  nullableReferenceTypes: true
  operationResultTask: true
  packageAuthors: me
  packageCopyright: 2025 Me
  packageDescription: An API server
  packageGuid: "{3D10FC07-0C87-4C7E-B168-D9729EE1C918}"
  packageName: ApiServer
  packageTitle: API Server
  packageVersion: "0.0.0"
  returnICollection: false
  schemaMappings:
    ProblemDetails: Microsoft.AspNetCore.Mvc.ProblemDetails
  useCollection: true
  useDateTimeOffset: true
  useNewtonsoft: true
  useSwashbuckle: false
  userSecretsGuid: 3d10fc07-0c87-4c7e-b168-d9729ee1c918
generatorName: aspnetcore
Related issues/PRs

I suspect the issue was introduced with #21600. I see a test was changed to actually expect this type declaration?

I don't see any recent changes to the model template, and the model template inserts the question mark after the type name. The template doesn't appear to have any knowledge of generic type arguments.

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

I think the question mark is being placed in between the type name and the generic type argument in the Java code?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions