Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #4816
Closed
Open
Issue created Dec 17, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][C#] Should use nullable types for non-required properties

Created by: kevinoid

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The C# generators currently generate model classes using non-nullable types for properties which are not required, which can't represent instances where those properties are not present.

openapi-generator version

v4.0.0 and later

OpenAPI declaration file content or url
Example OpenAPI 3.0.2 document
openapi: '3.0.2'
info:
  title: non-required property example
  version: '1.0.0'
components:
  schemas:
    DateRange:
      description: A possibly open-ended date range.
      type: object
      properties:
        start:
          type: string
          format: date-time
        end:
          type: string
          format: date-time
      required:
      - start
paths:
  /date-ranges:
    get:
      operationId: getDateRanges
      responses:
        default:
          description: Get date ranges
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/DateRange'
    post:
      operationId: addDateRange
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DateRange'
      responses:
        '201':
          description: Success

Note that end is not declared nullable: true because end is never null in the JSON produced or consumed by the API. It is either a date string, or not present.

Command line used for generation

java -jar openapi-generator-cli.jar generate -g csharp-netcore -i openapi.yaml -o generated

Steps to reproduce
  1. Ensure the API returns at least one open-ended range (i.e. a DateRange object without an end property).
  2. Call GetDateRanges and note that End for the open-ended range is DateTime(1900-01-01), which is problematic since it is indistinguishable from "end":"1900-01-01" and likely violates the constraint that End is not before Start.
  3. Note that there is no way to call AddDateRange with an open-ended range, since End will always have a value.
Related issues/PRs

The regression occurred between v3.0.2 and v4.0.0. Bisect says the first bad commit is 37442733 (v4.0.0), so I'm obviously doing something wrong. (Maybe cli is using published core of same version, rather than locally-built version?) Advice on how to bisect would be appreciated.

The issue was also discussed in https://github.com/OpenAPITools/openapi-generator/issues/3725#issuecomment-545039145.

Suggest a fix

I believe nullable types should be used for properties which are either nullable or not required, since null in C# is a reasonable representation of both JSON properties which are null and properties which are not present.

Thanks for considering, Kevin

Assignee
Assign to
Time tracking