Closed rdalverny closed 6 years ago
I don't like adapting to a server who doesn't follow its own specification. JSON has just 5 types (or 7 if you count null, and integer separately from number) ... and still people succeed to use the wrong ones :frowning_face:.
If at all, that kind of behavior should only be available as a (non-default) option.
I agree it's far from beautiful but:
The problem with Postel's principle (the part "be liberal in what you accept") is that people start to rely on the behavior. If data consumers start to accept strings instead of other values, then data producers will start to send strings where numbers are meant (without anyone noting that they are doing wrong things). And then other consumers break, so those need to be adapted too ... and so on.
This is why I would like to have this feature protected with some option, so it is not in widespread usage, but just used where we know it is needed (like your example).
Fully agree. Here's a possibility: https://github.com/rdalverny/swagger-codegen/commit/209c7c50ab506d19cd12f567d8d1dc335dc41a5d. Shall I push it as a PR or do you have any feedback before at this point (I'm not sure I did it all right regarding the option addition)?
Looks fine from the distance (I don't know Swift enough to be able to judge about that, and didn't load the Java part in the IDE).
Description
It happens that some server implementations send booleans or integers as strings, yet the client library expects a boolean or an integer (this is somewhat messy, agreed - in my case).
At this point, on such case, a generated Swift3 client will
fatalError("Source \(source) is not convertible to type \(clazz): Maybe swagger file is insufficient")
(in swift3/Models.mustache#L92).Well, the server implementations could be more up to spec, although that's not always the case. The client could just accept only strings and later do the conversion.
However, having the client library cast the server response in the correct type at once would be nice (the Java client library does this, as it relies on gson). More, this also allows the server implementations to stay as they are, or to get up to spec, without having to update the client code.
Swagger-codegen version
I am using the HEAD revision at this point.
Steps to reproduce
"123"
instead of123
, or"true"
instead oftrue
)Related issues
I am not sure, I'm new to this (Swift and Swagger). However:
2735, although closed, seems related; I am not sure the suggested solution (use "object" for arbitrary JSON data) is appropriate when we have simple types that may be trivially converted (client expecting an int, server sending an int encoded as a string);
5543 is, the other way around (converting a string into a boolean) - just for context; I don't think it has any significance here;
4382 is, in the way an mismatch is handled (fatalError)
Suggest a Fix
I am not sure this is the best way to fix this, or even if there would not be a better, more generic way in Swift. However the following comes to work on my side (apart from fixing the server implementations of course). Would using a specialized library be better (hkellaway/Gloss, ikesyo/Himotoki or other - at this point, I really have no idea)?