swagger-api / swagger-api/swagger-codegen
[Bug]: Cannot target java LocalDateTime via type mappings
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Using typeMappings I cannot specify a class with in-built support due to reserved word checking.
The OpenAPI specification defined "date-time" as a format value for strings, but that allows anything in ISO-8601. I want to declare in my spec whether it should be local date-time (no suffix), Zulu (Z suffix), or offer (+-nnnn suffix)
I am trying to do this with the following type mappings
typeMappings:
string+date-time-local: LocalDateTime
string+date: LocalDate
string+time: LocalTime
string+date-time-duration: Duration
string+date-period: Period
string+date-time-zoned: ZonedDateTime
string+date-time-offset: OffsetDateTime
string+timestamp: Instant
importMappings:
LocalDateTime: 'java.time.LocalDateTime'
LocalDate: 'java.time.LocalDate'
LocalTime: 'java.time.LocalTime'
ZonedDateTime: 'java.time.ZonedDateTime'
OffsetDateTime: 'java.time.OffsetDateTime'
Instant: 'java.time.Instant'
Duration: 'java.time.Duration'
Period: 'java.time.Period'
"LocalDate" is treated as a reserved word to avoid conflict between a spec declaring a model of that name and the java.time classes. I do not want to declare anything new but instead actually use the standard java.time classes.
When doing this, I receive this warning [WARNING] LocalDate (reserved word) cannot be used as model name. Renamed to ModelLocalDate, and it generates code using ModelLocalDate. I cannot see any obvious way of achieving what I need to do,.
Swagger Codegen Version
7.13.0 (via maven plugin)
Language / Generator
Java
OpenAPI/Swagger Spec
openapi: 3.1.0
info:
title: Example
version: 1.0.0
paths:
/example:
post:
summary: example
operationId: example
parameters:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Example"
components:
schemas:
Example:
type: object
properties:
localDate:
type: string
format: date-time-local
Steps to Reproduce
Attempt to generate the above OpenAPI specification using the type mappings above.
Expected Behaviour
Code is generated which utilised java.time.LocalDateTime
Actual Behavior
Code is generated using ModelLocalDateTime, which does not compile
Related
This would appear to be related to the changes in https://github.com/OpenAPITools/openapi-generator/pull/16276
Checklist
- I have searched the existing issues to make sure this is not a duplicate.
- I have included a minimal and reproducible spec example.
- I have explained how to reproduce the issue.
- I have specified which generator/language is affected.
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 issue with the provided OpenAPI 3.1 specification and the shown typeMappings and importMappings through the Maven plugin. Start by tracing the Java generator's reserved-word handling for mapped types; done means the generated code uses java.time.LocalDateTime rather than ModelLocalDateTime and compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100