Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Comments

Close side panel

Decode JSON input specs using YAMLDecoder#34

Merged
liamnichols merged 6 commits intoCreateAPI:mainCreateAPI/CreateAPI:mainfrom
liamnichols:ln/json-preserving-property-orderliamnichols/CreateAPI:ln/json-preserving-property-orderCopy head branch name to clipboard
Jun 10, 2022
Merged

Decode JSON input specs using YAMLDecoder#34
liamnichols merged 6 commits intoCreateAPI:mainCreateAPI/CreateAPI:mainfrom
liamnichols:ln/json-preserving-property-orderliamnichols/CreateAPI:ln/json-preserving-property-orderCopy head branch name to clipboard

Conversation

@liamnichols
Copy link
Member

@liamnichols liamnichols commented Apr 6, 2022

Background

When loading OpenAPI schemas that are encoded in JSON format, Generate will use JSONDecoder to load it as an OpenAPI.Document. This is inconvenient compared to yaml files for two reasons:

  1. YAMLDecoder is thread safe, meaning we can use ParallelDocumentParser to decode quicker
  2. JSONDecoder does not preserve the ordering of dictionaries that it deserialises resulting in unstable generated output unless you enable isSortingPropertiesAlphabetically

Since most common JSON is also valid YAML, we could save some code and also solve the two problems above by using YAMLDecoder when decoding json files as well.

How

In this PR, I pretty much do just that. In the changes to Generate.swift, I tweak parseInputSpec() so that we check the file format before loading the data and then I remove the call to JSONDecoder so that YAMLDecoder is used regardless of file format.

To support this change, I have also updated some of the test helper methods to require an ext parameter when loading/testing specs since I've also added cookpad.json, which is a really simple Open API schema we use for a small static API that makes up part of our hiring process.

While the schema I committed to the specs is quite small, I can also confirm that we've already been using this approach on a much larger schema for a few months now without issues. In addition, I also converted github.yaml to JSON and re-ran the tests to see if there are any changes - there weren't. If you'd like me to commit convert one of the bigger existing schemas to json as well, do let me know.


One thing to note however is this:

With the current versions of Yams, there is one bug that might come up if your schema has empty strings anywhere - they might be mistakenly considered as nil in some cases. This is actually why I didn't update readOptions() to use YAMLDecoder for json configs, since one in the specs falls victim of the bug.

Hopefully that patch will be merged relatively soon though and this won't be too much of a problem. Happy to put this PR on hold though if you'd prefer to wait for the Yams patch to land upstream first.

I merged this patch to our fork anyway.

@liamnichols
Copy link
Member Author

There were about 10 packages missing from Tests/GeneratedPackages.xcodeproj so I just deleted everything and dragged all 38 of them back in 899107f

@liamnichols liamnichols force-pushed the ln/json-preserving-property-order branch from 899107f to ee0d7d3 Compare June 9, 2022 21:20
@liamnichols liamnichols force-pushed the ln/json-preserving-property-order branch from ee0d7d3 to 463592d Compare June 9, 2022 21:27
@liamnichols liamnichols requested review from ainame and imjn June 9, 2022 21:34
@liamnichols liamnichols linked an issue Jun 9, 2022 that may be closed by this pull request
Copy link
Member

@imjn imjn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ready to be merged 👍🏽

@liamnichols liamnichols merged commit ae70840 into CreateAPI:main Jun 10, 2022
@liamnichols liamnichols deleted the ln/json-preserving-property-order branch June 10, 2022 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.