-
Notifications
You must be signed in to change notification settings - Fork 15
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
Allow sorting schema properties by name #78
base: main
Are you sure you want to change the base?
Conversation
Please could you clarify, how do you configure it so that:
and (2) is happening but (1) is not |
Thanks for the PR, I still have to find time to review the code and the impact of the breaking changes. |
Good catch @ed-randall-blk. |
7345a8c
to
32afa53
Compare
Ok, I added a sort implementation for arrays now, @ed-randall-blk (e.g. of parameters) You are right, @thim81, that the new test should demonstrate the added/changed behaviour. |
This is really good. I think it's doing exactly what we need, your efforts are much appreciated, thankyou. |
Two issues remain for our use-case:
|
You bring up some good ideas, @ed-randall-blk , but I think they would blow up the scope of this PR too much. Sorting paths could work similar to the component order. There the definitions are ordered by the key. Unfortunately the paths are not contained within the components. So we need some new idea that won't make the configuration much more complex. Not sure what is behind the second point, but I extpect it to be not too difficult to fix. Still better do it in a separate PR. |
Since writing that I have understood the behaviour of this better;
we can run our generated files through this and review/compare changes consistently much more easily than before. |
Following up on the progress and the findings.
|
Yes this PR is definitely still needed to be able to sort these things. I tried to keep the breakage to a minimum. What do you think, @thim81 ? |
The "swagger.json" (swagger 2.0) document that I'm working with seems fully compatible with the tool (for sorting purposes, at least) other than that it has "swagger: 2.0" instead of "openapi: 3.0" at the top; I tried to update the python to add both tags - but this fails the swagger validation :
So it would be useful if openapi-format would recognise either On the sorting front, yes we've been using a fork with @bodograumann 's patch applied to achieve stable ordering / normalization of the file. |
hi @bodograumann & @ed-randall-blk Re-opening the discussion. Have a look at the tests:
Given that this option, will this solve your use-case? |
I'll look into it, @thim81 , but it is really confusing when |
Let me be a bit more in-depth.
Going over the current
All in all I must say that this part of the code is quite convoluted and hard to work on. That is why I tried to clean it up a bit in this PR. The main point of this PR was to solve the issue in the last two blocks. Any order defined for Later, by @ed-randall-blk's request, I also added support for (1), which might have increased the scope of this PR a bit too much, I must admit. My array sorting also allows sorting by multiple fields, which again might be overkill. What do yo think about introducing a completely new sorting configuration, besides the current one?
Sidenote: |
Currently it is not possible to sort properties, schemas or responses by name, because any order defined for them is applied to the nested objects instead.
In order to distinguish between direct ordering of an object and nested ordering of its keys, I have introduced a new notation
properties[*]
here akin to JSONPath as used by Spectral.Ideally the sort order would use complete path expressions, but that will take much more effort.
I know this is a breaking change for anyone using a custom sort order, but I think it is worthwhile.
The current implementation is limited and I found it difficult to understand how a custom sortFile even works. First I used trial-and-error to see what can and can't be sorted and then I looked at the source code.
E.g. the default sort order defines
content: []
, which means sort anything under acontent
key alphabetically and it hasproperties: […]
, which means sort the fields of all objects nested inside theproperties
key.