swagger-api / swagger-api/swagger-codegen
[JAVA] Better type mapping support
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
My particular frustration is with the mapping of date types. I'm totally on board with format: date mapping to java.time.LocalDate and format: date-time mapping to java.time.OffsetDateTime. But sometimes I want to map to java.time.LocalDateTime. I know I can override the mapping with <typeMappings> but I want to be able to have a mix in my model. That is to say I want to have some properties that are java.time.LocalDate, some that are java.time.OffsetDateTime and some that are java.time.LocalDateTime. I can't do this at the moment because all date-time properties are mapped in the same way.
Swagger-codegen version
2.2.3
Suggest a fix/enhancement
swagger spec defines the date and date-time formats that we know and love, but also says that "format is an open value, so you can use any formats, even not those defined by the OpenAPI Specification"
My idea is to say in my swagger format: local-date-time then I would be able to tell the codegen to map that, e.g.: <formatMappings>local-date-time=java.time.LocalDateTime</formatMappings>
This would be much more flexible than the current way, and allow mapping of the examples given in the spec such as uuid, uri and even email if you had a custom email class.
I'd go as far to say that the current way of mapping all types would be better off this way, dates, numbers, everything. All the issues people raise about type mapping would go away, because they would be able to get the tool to do whatever they want.
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
The issue proposes configurable format-to-type mappings for generated Java models, including mixed date formats and custom formats. Start by locating the existing typeMappings handling and date/format mapping logic; done means formatMappings can map custom formats such as local-date-time while preserving distinct mappings within one model.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100