OpenAPITools / OpenAPITools/openapi-generator
Default value for date and date-time fields has no effect
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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