Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSharp Code Generation: GenerateOptionalPropertiesAsNullable is not respected for OAS Schema Components #1305

Open
Jesse0451 opened this issue Jan 17, 2021 · 3 comments

Comments

@Jesse0451
Copy link

Related to RicoSuter/NSwag#2313

Optional Scalar properties do not produce nullable C# built-in value types when the types are defined in components/schema.

openapi: '3.0.2'
info:
  title: API Title
  version: '1.0'
servers:
  - url: https://api.server.test/v1
paths:
  /test:
    post:
      requestBody:
        required: true
        content: 
         application/json:
            schema: 
              type: object
              properties:
                requiredInlineNumber:
                  type: integer
                  minimum: 1
                optionalInlineNumber:
                  type: integer
                  minimum: 1
                requiredSchemaNumber: 
                    $ref: "#/components/schemas/naturalNumber"
                optionalSchemaNumber: 
                    $ref: "#/components/schemas/naturalNumber"
              required: 
                - requiredInlineNumber
                - requiredSchemaNumber
              additionalProperties: false
      responses:
        '200':
          description: OK
components:
  schemas:      
    naturalNumber:
      type: integer
      minimum: 1

Generated C# class:

    public partial class Body 
    {
        [Newtonsoft.Json.JsonProperty("requiredInlineNumber", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]
        public int RequiredInlineNumber { get; set; }
    
        [Newtonsoft.Json.JsonProperty("optionalInlineNumber", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]
        public int? OptionalInlineNumber { get; set; }
    
        [Newtonsoft.Json.JsonProperty("requiredSchemaNumber", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]
        public int RequiredSchemaNumber { get; set; }
    
        [Newtonsoft.Json.JsonProperty("optionalSchemaNumber", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]
        public int OptionalSchemaNumber { get; set; }
    
    
    }

When GenerateOptionalPropertiesAsNullable is true, OptionalSchemaNumber is expected to be nullable (int?) as per OptionalInlineNumber.

@RicoSuter
Copy link
Owner

Touching nullability is very dangerous as it could break other things.. can you create a PR with a unit test reproducing this here (with a JSOn Schema)?

@tjmcdonough
Copy link

Is it possible to also GenerateOptionalPropertiesAsNullable when required is false? I am using an Open API schema where required is false but they haven't specified if the schema.nullable is true

@RicoSuter
Copy link
Owner

Might be fixed with the next version of NJS/NSwag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants