Code Generation: Non-Required Open Api 3.0 scalar properties are non nullable
See original GitHub issueI 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:
- Created 4 years ago
- Comments:14 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay, that looks like a good solution. Maybe GenerateOptionalPropertiesAsNullable is a better name for the setting?
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?