-
Notifications
You must be signed in to change notification settings - Fork 12
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
Not possible to generate a struct with attributes in the same order as the API specs #105
Comments
Hi, can I ask why you would need that? I understand your feature request perfectly but it reads to me like a XY problem. Is it just about the order of the actual fields? Because at the end of the day the JSON will still have roughly random order fields, and if it's about malign we can run that on the generated code. When you use the struct, you can also use any order so I'm failing to see the value behind such a feature request. |
Hi, thanks for your prompt response. The intention was to use the generated model directly to generate an HTTP response (json API) from the server code and I wanted to have some control over the order of the fields. In my particular case, it is not a big deal since I can take care of the ordering in the UI code or even map it to another struct on the server side if needed. When I was investigating this I found it interesting that there could be optimisations done by sorting struct attributes, see this But anyway, I'm happy to close this issue. Thanks 👍 |
Go actually has a I forgot exactly if there is a provided auto-fix for it, but adding this code to the ouput pipeline is not something I'm against. We explicitly do not guarantee order anywhere, we just sort the fields as part of pre-passes to make the output deterministic. Unfortunately the openAPI spec represents them as a map so we'd need to switch to ordered maps. I think the openapi library we use did the change on their end but it'd be quite a big refactoring on our end. I do not mind either (or even both options). Open to contributions, in the best case scenario it's a small 10 line changes somewhere here https://github.com/discord-gophers/goapi-gen/blob/main/codegen/codegen.go#L227-L235 |
Hey folks! If you don't mind, I will express my support in favor to @yeforriak's suggestion. In my opinion, maintaining the order of properties as defined in the API specs would enhances readability and helps developers keep related data together. I couldn't think in a better example, but, like the example below, the ID would be "out of place" alphabeticaly ordered.
Data grouping is valuable during development, especially when working on database modeling. Implementing this change would improve the developer experience keeping the code intuitive. |
I'm currently working on implementing this feature. Here is my ongoing PR: #106. |
I want to generate a struct with attributes ordered as defined in the OpenAPI specification doc.
Api specification:
The generated struct attributes are sorted alphabetically:
but I would like the struct to be defined as
The text was updated successfully, but these errors were encountered: