You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
For types such as dictionary or list, there is no default value which then forces it to be required even though the EmitDefaultValue is set to true. What is needed is to set the property for these types like the below for collection types.
Issue
Collections (like Dictionary, List, etc.) do not have a natural default value in C#. Without initialization, they remain null. This causes an issue when you serialize or deserialize these types. Even if EmitDefaultValue = true is set, the property still remains null unless explicitly initialized in code.
openapi-generator version
7.11.0-SNAPSHOT
OpenAPI declaration file content or url
openapi: 3.0.0info:
title: Test Dictionary Nullabilitydescription: Test Dictionary Nullabilityversion: '2025-01-31'tags: []paths: {}components:
schemas:
JobDefinition:
type: objectproperties:
JobName:
type: stringdescription: Name of the JobreadOnly: trueSparkConf:
type: objectadditionalProperties:
type: stringnullable: truedescription: The key value pairs for spark configuration.
/* * Test Dictionary Nullability * * Test Dictionary Nullability * * The version of the OpenAPI document: 2025-01-31 * Generated by: https://github.com/openapitools/openapi-generator.git */usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Collections.ObjectModel;usingSystem.Linq;usingSystem.IO;usingSystem.Runtime.Serialization;usingSystem.Text;usingSystem.Text.RegularExpressions;usingNewtonsoft.Json;usingNewtonsoft.Json.Converters;usingNewtonsoft.Json.Linq;usingSystem.ComponentModel.DataAnnotations;usingOpenAPIDateConverter=Microsoft.SentinelGraph.JobService.Client.OpenAPIDateConverter;namespaceMicrosoft.SentinelGraph.JobService.Model{/// <summary>/// JobDefinition/// </summary>[DataContract(Name="JobDefinition")]publicpartialclassJobDefinition:IValidatableObject{/// <summary>/// Initializes a new instance of the <see cref="JobDefinition" /> class./// </summary>/// <param name="sparkConf">The key value pairs for spark configuration..</param>publicJobDefinition(Dictionary<string,string>sparkConf=default(Dictionary<string,string>)){this.SparkConf=sparkConf;}/// <summary>/// Name of the Job/// </summary>/// <value>Name of the Job</value>[DataMember(Name="JobName",EmitDefaultValue=false)]publicstringJobName{get;privateset;}/// <summary>/// Returns false as JobName should not be serialized given that it's read-only./// </summary>/// <returns>false (boolean)</returns>publicboolShouldSerializeJobName(){returnfalse;}/// <summary>/// The key value pairs for spark configuration./// </summary>/// <value>The key value pairs for spark configuration.</value>[DataMember(Name="SparkConf",EmitDefaultValue=true)]publicDictionary<string,string>SparkConf{get;set;}/// <summary>/// Returns the string presentation of the object/// </summary>/// <returns>String presentation of the object</returns>publicoverridestringToString(){StringBuildersb=newStringBuilder();sb.Append("class JobDefinition {\n");sb.Append(" JobName: ").Append(JobName).Append("\n");sb.Append(" SparkConf: ").Append(SparkConf).Append("\n");sb.Append("}\n");returnsb.ToString();}/// <summary>/// Returns the JSON string presentation of the object/// </summary>/// <returns>JSON string presentation of the object</returns>publicvirtualstringToJson(){returnNewtonsoft.Json.JsonConvert.SerializeObject(this,Newtonsoft.Json.Formatting.Indented);}/// <summary>/// To validate all properties of the instance/// </summary>/// <param name="validationContext">Validation context</param>/// <returns>Validation Result</returns>IEnumerable<ValidationResult>IValidatableObject.Validate(ValidationContextvalidationContext){yieldbreak;}}}
Request Body
{
"JobName": "demo"
}
Error Response
{
"errors": {
"sparkConf": [
"The SparkConf field is required."
]
},
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-89d8bac983fa49cfaa3b35d2ac8fb577-99b716e53e94e3f7-00"
}
Related issues/PRs
Suggest a fix
To ensure that collections are never null, you must initialize the property (either in the field, in the constructor, or via a custom setter). This ensures that property will always be an empty dictionary (new Dictionary<string, string>()) instead of null, even if it's not included in the request payload. This initialization avoids the issue where the property is "missing" and treated as required, triggering a 400 Bad Request.
If using [] I think it can be applied like the below. Unsure how to specify for language >= net5, otherwise specify the same way type was included in the method return type. (it would be nice to use language supported features where possible)
If vendorExtensions.x-emit-default-value is true, we emit = [].
If vendorExtensions.x-emit-default-value is not true and required is true, we emit = [].
If neither condition is true, nothing will be emitted.
Note since .net5, the empty collection format has been supported. A check can be made for any collection types that are known to be auto generated (List, Dictionary, HashSet) and applied.
The text was updated successfully, but these errors were encountered:
Bug Report Checklist
Description
For types such as dictionary or list, there is no default value which then forces it to be required even though the EmitDefaultValue is set to true. What is needed is to set the property for these types like the below for collection types.
Issue
Collections (like Dictionary, List, etc.) do not have a natural default value in C#. Without initialization, they remain null. This causes an issue when you serialize or deserialize these types. Even if EmitDefaultValue = true is set, the property still remains null unless explicitly initialized in code.
openapi-generator version
7.11.0-SNAPSHOT
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
JobDefinition.cs
Request Body
Error Response
Related issues/PRs
Suggest a fix
To ensure that collections are never null, you must initialize the property (either in the field, in the constructor, or via a custom setter). This ensures that property will always be an empty dictionary (new Dictionary<string, string>()) instead of null, even if it's not included in the request payload. This initialization avoids the issue where the property is "missing" and treated as required, triggering a 400 Bad Request.
If using
[]
I think it can be applied like the below. Unsure how to specify for language >= net5, otherwise specify the same way type was included in the method return type. (it would be nice to use language supported features where possible)modelGeneric.mustache#L54
Note since .net5, the empty collection format has been supported. A check can be made for any collection types that are known to be auto generated (List, Dictionary, HashSet) and applied.
The text was updated successfully, but these errors were encountered: