-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG][Swift] Decimal-Decoding not working #13410
Comments
@Feuerwerk I'm using 6.0.0 and I'm not running into this issue. Did you try with that version? |
@otusweb I downgraded openapi-generator to 6.0.0 but got exactly the same problem. Add the 4 methods for encoding/decoding also solves the problem. |
Hi @Feuerwerk. Does this also work if there are attributes like this in the swagger?
Because I guess with your change they would now also be decoded from a string (which it is not) and encoded to strings which will both fail |
@Feuerwerk
I guess in the swagger file that you sent, there is no indication that you want a decimal, just that it should be a number. |
Hi @Jonas1893
Al least from my understanding of the OpenAPI Spec 3.0 type number / format double results in a Double datatype not in a Decimal datatype. I also tried it with openapi-generator (6.2.0) and the datamodel of my Swift client now has the following property:
And in this case the workaround (solution?) I suggested for Decimal would not interfere with Double. |
Hi @otusweb
Sadly "decimal" is not specified in OpenAPI Spec 3.0 but due to #7808 i thought type string / format number is the way to go. The Apples Swift JsonParser can't handle Decimal from a string without additional support I think. |
Exactly. In the example I described the json response from the server will not be
like in your example but
And because of your extension decoder would now try to decode from a String, fail and throw the exception, no? Maybe I am overlooking something |
@Jonas1893 That are different datatypes (Double vs. Decimal) and they can / will be treated differently by Apples Json-Decoder / -Encoder. The code I suggested only extend encoding / decoding for swift type Decimal. But if the data model contains the swift type Double (like in your example) the code I suggested simply will not run and therefore will not interfere and cause problems. |
Understood, thx for clarifying |
I'm pretty sure that @wing328 who added support for Decimal in Swift-Clients in OpenAPI Generator (type string / format number) did some testing so why didn't he get errors and you don't get errors too but I'm keep getting exceptions? |
@Feuerwerk On our end, here is the json spec for a decimal number:
and the data returned by the server for that value: I'm running 6.0.0 and still on Xcode 13, but I'm assuming that the version of Xcode should not have much impact. Maybe iOS version. I've tested on iOS 15 and lower, but not 16 yet. I don't know how the internals work though, just that this combo works for us. |
@otusweb Ok, that will explain why it's working for you. You signal in your service description that the server will send a string that should be interpreted as a decimal. Therefore OpenAPI Generator will generate a data model with the property PriceIncVat of type Decimal. But your server will send the data actually as a number. On Swift-Client side the received data will be parsed by Apples JsonDecoder which has a default implementation for Decimal and expect a number in that case (not a string). It simply works by accident for you. |
@Feuerwerk thanks for pointing that out. Sounds like a larger issue for us then :-) |
* [swift5] fixes bug #13410 * Fixed indentation, added missing generated samples
Description
I have a service providing a list of objects with a property of type string and format number (BigDecimal on the backend side). The generated service description is fine:
The models generated by OpenAPI-Generator also looks good:
But when my swift5 client tries to consume this service I always get the exception that the returning data is not valid and can't be decoded.
I checked the data transmitted from the server and it looks valid to me.
When sending data from the swift client to the server, it will encode the decimal as a number
openapi-generator version
6.1.0
Generation Details
openapi-generator generate -i http://localhost:8081/v3/api-docs -g swift5 -c config.json
Suggest a fix
I can fix the problem when I add the following four methods to the generated Extensions.swift
The text was updated successfully, but these errors were encountered: