You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Option to generate a struct without sorting fields #458
I want to generate a struct without sorting fields, in the order listed in the OpenAPI doc.
I wrote my OpenAPI component schema so that the fields that are important to the user are at the top
And I want to return JSON fields on my server response in the same order.
However, for now, generated fields are sorted in increasing order.
So I want an option to generate a struct without sorting field.
openapi.yml
components:
schemas:
Pet:
properties:
id:
type: integerformat: int64description: Unique id of the petname:
type: stringdescription: Name of the petage:
type: integerformat: int64description: Age of the pet
Current
// Pet defines model for Pet.typePetstruct {
// Age of the petAge*int64`json:"age,omitempty"`// Unique id of the petId*int64`json:"id,omitempty"`// Name of the petName*string`json:"name,omitempty"`
}
What I want
// Pet defines model for Pet.typePetstruct {
// Unique id of the petId*int64`json:"id,omitempty"`// Name of the petName*string`json:"name,omitempty"`// Age of the petAge*int64`json:"age,omitempty"`
}
Reacted by Nikita, Mike, Vojtech Pavlovsky, Tibor Blaho, Serhii Novachenko, h1k3rr, Gergely Novák, Nikita Kazarian, Jarrett Vance, Ori Shalom and 22 more
I have check dependecy for parsing openapi specification - github.com/getkin/kin-openapi
openapi3/schema.go:156 - field that keep properties
openapi3/loader.go:159 - unmarshalling yaml to struct
Look like it is a behaviour of "github.com/invopop/yaml" unmarshalling mechanism.
I tried "gopkg.in/yaml.v3" , but does not work.
What suggestions how to fix it without writing a new yaml parser?
Activity
weusego commentedon Mar 25, 2022
+100 -
great job with oapi-codegen, but we are very irritated to see all the fields from our nicely designed structures appear in alphabetical order...
the underlying https://github.com/getkin/kin-openapi/ seems to be the problem, parsing it into a map where the ordering is lost.
gnvk commentedon Jun 2, 2022
@deepmap-marcinr Is there a way to fix / work around this?
Add support for the x-order extension.
Add support for the x-order extension.
psyhatter commentedon Jun 14, 2022
Often this problem is solved by the
x-orderextension, for example in https://github.com/go-openapi/spec or https://github.com/go-swagger/go-swaggerAdd support for the x-order extension.
goforbroke1006 commentedon Jun 15, 2022
I have check dependecy for parsing openapi specification - github.com/getkin/kin-openapi
openapi3/schema.go:156 - field that keep properties
openapi3/loader.go:159 - unmarshalling yaml to struct
Look like it is a behaviour of "github.com/invopop/yaml" unmarshalling mechanism.
I tried "gopkg.in/yaml.v3" , but does not work.
What suggestions how to fix it without writing a new yaml parser?
renom commentedon Sep 14, 2023
https://github.com/getkin/kin-openapi did use https://github.com/ghodss/yaml but they switched to https://github.com/invopop/yaml for some reason.
https://github.com/ghodss/yaml seems to keep the order of the fields when converted.
renom commentedon Sep 18, 2023
Well, it's somewhere inside convertToJSONableObject in invopop/yaml because go-yaml/yaml keeps the ordering when YAML is converted to map.
renom commentedon Oct 19, 2023
https://github.com/goccy/go-yaml
This library preserves the order. Would it be enough to replace invopop/yaml with goccy/go-yaml? It has the same function
func YAMLToJSON(bytes []byte) ([]byte, error)DeOne4eg commentedon Dec 17, 2023
The problem is relevant for me too. Are there any solutions?
stijnderyckere commentedon Jan 18, 2024
This also frustrates me the most about the oapi codegen. Are there any workarounds for this?
jamietanna commentedon Jan 25, 2024
👀 #1190 should be merged today
jamietanna commentedon Jan 25, 2024
Closed by 3bf5755 (incorrectly attributed commit)