Skip to content

[Multiple languages] properties are generated as nullable/optional, despite required/nullable usage in OAS schema #3911

@bkoelman

Description

@bkoelman

Kiota ignores the usage of required/nullable in OAS schemas. C# properties are always generated as nullable. This results in unneeded noisy squiggles, as shown here.

NSwag does a better job, by performing the following translation (using the /GenerateNullableReferenceTypes:true switch):

  • OAS non-required + nullable: C# nullable
  • OAS required + nullable: C# nullable
  • OAS non-required + non-nullable: C# non-nullable, unless combined with the /GenerateOptionalPropertiesAsNullable:true switch
  • OAS required + non-nullable: C# non-nullable

Additionally, NSwag adds [Newtonsoft.Json.JsonProperty(Required = ..., , NullValueHandling = ...)] on the generated properties, which results in client-side serialization errors (thus avoiding a server roundtrip on invalid input).

Example component schema
"exampleComponent": {
  "required": [
    "requiredNonNullableReferenceType",
    "requiredNullableReferenceType",
    "requiredNullableValueType",
    "requiredValueType"
  ],
  "type": "object",
  "properties": {
    "nonNullableReferenceType": {
      "type": "string"
    },
    "requiredNonNullableReferenceType": {
      "type": "string"
    },
    "nullableReferenceType": {
      "type": "string",
      "nullable": true
    },
    "requiredNullableReferenceType": {
      "type": "string",
      "nullable": true
    },
    "valueType": {
      "type": "integer",
      "format": "int32"
    },
    "requiredValueType": {
      "type": "integer",
      "format": "int32"
    },
    "nullableValueType": {
      "type": "integer",
      "format": "int32",
      "nullable": true
    },
    "requiredNullableValueType": {
      "type": "integer",
      "format": "int32",
      "nullable": true
    }
  },
  "additionalProperties": false
}

The motivation for this behavior in Kiota is provided here and here. I find it unfortunate that correct server implementations have to suffer from this.

Please upvote this issue if you'd like Kiota to respect the required/nullable usage in your OAS.

Metadata

Metadata

Labels

CsharpPull requests that update .net codePythonenhancementNew feature or request

Type

No type

Projects

Status

In Design 🎨

Relationships

None yet

Development

No branches or pull requests

Issue actions