#StackBounty: #swagger #openapi #swagger-codegen #openapi-generator How can I get OpenApi-Generator to produce nullable DateTimeOffset?

Bounty: 300

My API exposes some endpoints with DateTime? and others with DateTimeOffset?.

I want to generate an API Client using OpenApi-Generator, that will create client code for each endpoint, respecting the differences between the types.

OpenApi-Generator offers the option to useDateTimeOffset=true, which will generate the client using DateTimeOffset everywhere regardless of whether the API was exposing DateTime or DateTimeOffset. As such, I don’t want to use that.

So to resolve the requirement without that, I’m attempting to create a Filter in which I will specify that in the case of DateTimeOffset? the generator should use the type DateTimeOffset and make it nullable.

This is the Filter:

public class FixDateTimeOffsetTypeSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema model, SchemaFilterContext context)
    {
        if (context.Type == typeof(DateTimeOffset?))
        {
            model.Format = "date-time-offset-nullable";
            model.Type = "DateTimeOffset"; 
            model.Nullable = true;
        }
    }
}

and this is an example of the output from that:

"/Api/MyApiEndpoint": {
  "get": {
    "tags": [
      "General"
    ],
    "operationId": "General_MyApiEndpoint",
    "parameters": [
      {
        "name": "startDatetime",
        "in": "query",
        "schema": {
          "type": "DateTimeOffset",
          "format": "date-time-offset-nullable",
          "nullable": true
        }
      },

When I run this and generate the client from that JSON, the the DateTimeOffset objects are never nullable. It doesn’t seem to respect that instruction.

What do I need to change to make it work, so that when I specify DateTimeOffset should be nullable, it appears as nullable in the code?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.