Skip to content

[BUG] [CSharp] optional properties should use boxed type #19461

@marcel-huber-ptv

Description

@marcel-huber-ptv

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

As far as I could see, most other generators use boxed types instead of their primitive counterpart. In the CSharp generators, boxed types are only used, when nullable is set to true. Given an optional property that is not nullable, there is no way to see if it is provided in the request, or if it is not and the default value for the type was used.
The primitive datatype should only be used for required, non-nullable properties. In other cases, the boxed type should be used to allow properties to be null if they are not set as it is also the case for most other languages.

openapi-generator version

7.8.0

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: title
  version: 0.0.1
paths:
  /test:
    get:
      parameters:
        - name: deepObject
          in: query
          schema:
            $ref: '#/components/schemas/DeepObject'
          explode: true
          style: deepObject
      responses:
        204:
          description: no content
components:
  schemas:
    DeepObject:
      type: object
      required:
        - requiredProperty
      properties:
        requiredProperty:
          type: integer
          format: int32
        optionalProperty:
          type: integer
          format: int32
Generation Details

This will generate:

public DeepObject(int requiredProperty = default(int), int optionalProperty = default(int))
{
    this.RequiredProperty = requiredProperty;
    this.OptionalProperty = optionalProperty;
}

but expected is

public DeepObject(int requiredProperty = default(int), int? optionalProperty = default(int?))
{
    this.RequiredProperty = requiredProperty;
    this.OptionalProperty = optionalProperty;
}
Steps to reproduce
npm install @openapitools/openapi-generator-cli -g
openapi-generator-cli version-manager set 7.8.0
npx @openapitools/openapi-generator-cli generate -i ./openapi.yaml -g csharp
Related issues/PRs

couldn't find any

Suggest a fix

#19463

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