openapi-generator
openapi-generator copied to clipboard
[BUG][Swift] Template does not decode strings in date-only format
Bug Report Checklist
- [ ] Have you provided a full/minimal spec to reproduce the issue?
- [ ] Have you validated the input using an OpenAPI validator (example)?
- [x] What's the version of OpenAPI Generator used?
- [x] Have you search for related issues/PRs?
- [ ] What's the actual output vs expected output?
- [ ] [Optional] Bounty to sponsor the fix (example)
Description
The OpenAPI specs provides examples for date format including date and date-time
When generating a swift4 or swift5 client, the date format is hard-coded in the Configuration.mustache template. This is problematic for two reasons:
- It does assume only date-time is used, and must be configured otherwise manually before using the client
- Some API uses both formats to represent date and date and time, thus overriding it in one place would not help
openapi-generator version
4.2.3
Command line used for generation
openapi-generator generate -i input/specs.yaml -g swift4 -o output
# or
openapi-generator generate -i input/specs.yaml -g swift5 -o output
Steps to reproduce
generate a swift client
Suggest a fix
provide two properties with default values in the template and use them accordingly to decode date or date time strings
open class Configuration {
public static var dateFormat = "yyyy-MM-dd"
public static var dateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
}
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
Ran into the same problem! Any solutions?
We were running into the same issue. Any progress? Our current approach is to provide a custom DateFormatter via CodableHelper.dateFormatter which can handle both cases.
We were running into the same issue. Any progress? Our current approach is to provide a custom DateFormatter via CodableHelper.dateFormatter which can handle both cases.
@honkmaster How are you handling both cases?
I'm having exactly the same problem, but on the encoding side.
The API I'm calling has some date, some date-time, so I don't think I can fully override the dateFormatter?
Is there any solution to this in more recent versions of OAG?
I'm having exactly the same problem, but on the encoding side. The API I'm calling has some date, some date-time, so I don't think I can fully override the
dateFormatter? Is there any solution to this in more recent versions of OAG?
There is no solution yet unfortunately. We also went with the custom dateFormatter that tries to decode a date first with time information and after this fails without time information. But this is only a solution for the 'decoding' and not 'encoding' of dates since in encoding case we can not guess.
For the encoding case: I think the problem is that both date and date-time are mapped to Swifts Date type in the API models and afterwards in CodableHelper there is no longer the information available whether it should be encoded to a "date" or "date-time". What should probably happen is that the information whether we need to serialize Date to either "date" or "date-time" is somehow passed on to CodableHelper.
Or generator maps a "date" type to Swifts DateComponents or a custom DateOnly type both using calendar iso8601 for en- decoding.
Not sure what is the best approach
With #13520 OpenAPI specs date is being decoded correctly now in response models.
Also new generator flag useCustomDateWithoutTime has been added which will provide a new Data type in the API models as a replacement for Swift Date that enables generator to encode to date in your request models.
@4brunu how do we proceed with this ticket?
We can close it. If anyone still has an issue, please open a new ticket
Sorry, i know this already close. there is a similar issue in encoding. In "JSON" request, Date if not formatted with time even though the format in the spec is "format: date-time".
Hi, can you please share a sample project that shows the issue please? Thanks