Add a hint when decoding allowedValues #245
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've tested OpenAPIKit with over 400k lines of OpenAPI specs. The most common issue I've encountered so far has to do with string enums and allowed values and the interplay of
OpenAPIKit.AnyCodable
,Yams
, andDouble
. Here are a couple of examples:Issues
Jira
I get
AnyCodable
with value0
of typeInt
in the allowed values.It happens because of the sexagesimal support in Yams. When it sees ":" in a value, it assumes it's a number. I think it's supposed to bail out when it sees it has letters, but it doesn't and returns
0
.Sexagesimal is just one of the crazy pre-2009 YAML features and I'm not sure why it's supported by Yams. I'm currently using a fork in my project.
Square
I get
AnyCodable
with valueDouble.infinity
in the allowed values.This is a similar problem, but this time it's
AnyCodable
that causes an issue. YAML doesn't enforce strings to have quotes. So whenAnyCodable
callsif let double = try? container.decode(Double.self) {
, Yams attempts to initialize aDouble
with the given string. AndDouble("inf")
returnsDouble.infinity
.Suggested Fix
I suggest to add a hint when decoding
allowedValues
. When we know thatFormat.self
isJSONTypeFormat.StringFormat.self
, we can decode[String].self
instead of[AnyCodable].self
. It's more reliable and is better from the performance perspective as well.