-
Notifications
You must be signed in to change notification settings - Fork 21
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
Sort routes by path before writing the output JSON in openapi-generator #925
Conversation
821537e
to
d0b9ac6
Compare
d0b9ac6
to
32dab4d
Compare
.sort((a, b) => a.localeCompare(b)) | ||
.reduce( | ||
(acc, key) => { | ||
acc[key] = paths[key]!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works because insertion order is preserved for string keys
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, yes I see this in the comment you linked:
entries come in the order in which the properties were added
Followed immediately by:
Relying on object property ordering in JavaScript is a really bad idea, because it makes code extremely fragile. If you need an ordering, create an array with the properties in it in the order that works for your application.
😅
How do you feel about using a Map instead of an object? The overview of Map on MDN reads:
The Map object holds key-value pairs and remembers the original insertion order of the keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can try and see how it serializes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No luck:
YAMLException: unacceptable kind of an object to dump [object Map]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js-yaml
has a sortKeys option but that sorts all the keys and not just the ones under paths
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All righty, well, so long as it's tested and deterministic!
32dab4d
to
74395e1
Compare
BREAKING CHANGE: Changing output order of routes may affect consumers
74395e1
to
b014517
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorts route by path
🎉 This PR is included in version @api-ts/[email protected] 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
🎉 This PR is included in version @api-ts/[email protected] 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
🎉 This PR is included in version @api-ts/[email protected] 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
🎉 This PR is included in version @api-ts/[email protected] 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
Sort routes by path before writing the output JSON in
openapi-generator
.convertRoutesToOpenAPI
function inpackages/openapi-generator/src/openapi.ts
to sort routes by path before constructing thepaths
object.packages/openapi-generator/test/openapi/base.test.ts
to verify routes are sorted by path in the output JSON.BREAKING CHANGE: Changing the sort order may affect consumers that review diffs of the generated JSON