OpenAPITools / OpenAPITools/openapi-generator

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

Open
#14,305 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C# code generation path that processes custom type mappings and nullable OpenAPI schemas, using the provided TimeSpan and TimeSpan? mappings as the reproduction. Compare the generated declarations for startTime and endTime; done means the nullable schema produces a nullable C# property while the non-nullable schema remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.