OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.98k stars 6.6k forks source link

[BUG] C# CodeGen doesnt respect nullable value for custom mapping with timespan #14305

Open aswinfrancis91 opened 1 year ago

aswinfrancis91 commented 1 year ago
Description

I have below two properties

[Required]
public TimeSpan StartTime { get; set; }
public TimeSpan? EndTime { get; set; }

but since I can't generate SDK with same timespan field from the swagger json I added below line to startup class

c.MapType<TimeSpan>(() => new OpenApiSchema { Type = "string", Format = "time-span", Nullable = false });
c.MapType<TimeSpan?>(() => new OpenApiSchema { Type = "string", Format = "time-span", Nullable = true });

The json generated also looks good

"startTime": {
            "type": "string",
            "format": "time-span"
          },
 "endTime": {
            "type": "string",
            "format": "time-span",
            "nullable": true
          },

I then pass the below type mapping argument --type-mappings time-span=TimeSpan

The problem here is that the non nullable fields comes fine in the SDK generated but the nullable field comes as non nullable itself. Below is the C# code that was generated

[DataMember(Name = "startTime", IsRequired = true, EmitDefaultValue = true)]
public TimeSpan StartTime { get; set; }

[DataMember(Name = "endTime", EmitDefaultValue = true)]
 public TimeSpan EndTime { get; set; }
openapi-generator version

6.2.1

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/4951

aswinfrancis91 commented 6 months ago

Anyone looking into this?

sharifahmad2061 commented 2 weeks ago

No solution yet.