question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code Generation: Non-Required Open Api 3.0 scalar properties are non nullable

See original GitHub issue

I defined an Open API 3.0 spec with optional parameters. Optional parameters mean in my understanding, that they are nullable in C# and omitted in JSON serialization if they are null in C#.

But when I generate C# code, now the generated properties are never nullable, and I have no idea how to specify now not to serialize them. Am I doing something wrong or is this a major issue?

If I set the same spec to Open API 2.0, it behaves like I expect. The integers get generated as int? as I expect.

See this spec:

openapi: "3.0.0"
servers:
  - url: //petstore.swagger.io/v2
    description: Default server

components:
  schemas:
  
    Pet:
      type: object
      required:
        - id

      properties:
        id:
          type: integer
          
        id2:
          type: integer

Generated code:

//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v13.0.2.0 (NJsonSchema v10.0.20.0 (Newtonsoft.Json v11.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------


    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.0.20.0 (Newtonsoft.Json v11.0.0.0)")]
    public partial class Pet 
    {
        [Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.Always)]
        public int Id { get; set; }
    
        [Newtonsoft.Json.JsonProperty("id2", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public int Id2 { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }
    
    
    }

I would like to send this Json as a response (set required id and leave out id2)

{
  "id" : 20
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
timmvonputtkamercommented, Aug 22, 2019

Okay, that looks like a good solution. Maybe GenerateOptionalPropertiesAsNullable is a better name for the setting?

2reactions
RicoSutercommented, Jul 30, 2019

But yes you could argue that non-required properties also should be nullable and just ignored when null whereas nullable properties should serialize “null”, right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mark strings as non-nullable in ASP.NET Core 3.0 Swagger
To specify that an optional string is not nullable, you need to add [JsonProperty(Required = Required.DisallowNull)] to the property.
Read more >
OpenAPI Specification - Version 3.0.3
Keys used in YAML maps MUST be limited to a scalar string, as defined by the YAML Failsafe schema ruleset. Note: While APIs...
Read more >
OpenAPI Specification v3.0.3 | Introduction, Definitions, & ...
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.
Read more >
Language Guide (proto 3) | Protocol Buffers Documentation
This topic covers how to use the version 3 of Protocol Buffers in your project. This guide describes how to use the protocol...
Read more >
Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found