OpenAPITools / OpenAPITools/openapi-generator

Default value for date and date-time fields has no effect

Open
#1,299 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

An OpenAPI specification containing default values for date or date-time fields will have no effect on the generated code.

openapi-generator version

3.3.1

OpenAPI declaration file content or url
{
    "openapi": "3.0.0",
    "info": {"title":"blah", "version": "1.0.0"},
    "paths": {
        "/test1": {
            "post": {
                "tags": ["test"],
                "operationId": "testOp1",
                "responses": {
                    "200": {
                        "description": "successful operation"
                    }
                },
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/Request1"
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Request1": {
                "properties": {
                    "date_field": { 
                        "type":"string",
                        "format":"date",
                        "default":"2001-01-02"
                    },
                    "datetime_field": { 
                        "type":"string",
                        "format":"date-time",
                        "default":"2001-01-02T03:04:05.006"
                    }
                }
            }
        }
    }
}
Command line used for generation

It doesn't really matter what generator is used.

generate -v -g java -i foo.json -c rpdmcpp.config -o f:/temp/generated 
Steps to reproduce

Simple test:

  • Run the generator command line
  • Observe that generated Request1 date and dateTime fields are null:
    private LocalDate dateField = null;
    private OffsetDateTime datetimeField = null;

In-depth test:

  • Run the generator command line in debugger.
  • Break in Schema.setDefault()
public void setDefault(Object _default) {
    this._default = cast(_default);
}
  • Note that _default has a valid string like "2001-01-02"
  • Step into cast()
  • It will fail to convert because !(String instanceof Date)
Related issues/PRs

None that I know of

Suggest a fix/enhancement

I don't know where to start. It seems that the bug is in a swagger library dependency not in openapi-generator itself. Perhaps there is a workaround to force a conversion?

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

Reproduce the Java generation command with the supplied OpenAPI declaration, then inspect Schema.setDefault() and cast(), where the report observes the string-to-date conversion failure. Trace how the generated Request1 date and dateTime fields receive defaults; done means they are initialized to the declared date and date-time values rather than null.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.