Skip to content
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

Option to generate a struct without sorting fields #458

Closed
mf-sakura opened this issue Oct 7, 2021 · 11 comments
Closed

Option to generate a struct without sorting fields #458

mf-sakura opened this issue Oct 7, 2021 · 11 comments

Comments

@mf-sakura
Copy link

mf-sakura commented Oct 7, 2021

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: integer
          format: int64
          description: Unique id of the pet
        name:
          type: string
          description: Name of the pet
        age:
          type: integer
          format: int64
          description: Age of the pet

Current

// Pet defines model for Pet.
type Pet struct {
  // Age of the pet
  Age *int64 `json:"age,omitempty"`

  // Unique id of the pet
  Id *int64 `json:"id,omitempty"`

  // Name of the pet
  Name *string `json:"name,omitempty"`
}

What I want

// Pet defines model for Pet.
type Pet struct {
  // Unique id of the pet
  Id *int64 `json:"id,omitempty"`

  // Name of the pet
  Name *string `json:"name,omitempty"`

  // Age of the pet
  Age *int64 `json:"age,omitempty"`
}
@weusego
Copy link

weusego commented 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
Copy link

gnvk commented Jun 2, 2022

@deepmap-marcinr Is there a way to fix / work around this?

psyhatter added a commit to psyhatter/oapi-codegen that referenced this issue Jun 13, 2022
Fix for deepmap#458.
Can be used when you need to set the order of fields in the model:
```yaml
    NewPet:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          x-order: 1
        tag:
          type: string
          x-order: 2
```
psyhatter added a commit to psyhatter/oapi-codegen that referenced this issue Jun 14, 2022
Fix for deepmap#458.
Can be used when you need to set the order of fields in the model:
```yaml
    NewPet:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          x-order: 1
        tag:
          type: string
          x-order: 2
```
@psyhatter
Copy link
Contributor

Often this problem is solved by the x-order extension, for example in https://github.com/go-openapi/spec or https://github.com/go-swagger/go-swagger

psyhatter added a commit to psyhatter/oapi-codegen that referenced this issue Jun 14, 2022
Fix for deepmap#458.
Can be used when you need to set the order of fields in the model:
```yaml
    NewPet:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          x-order: 1
        tag:
          type: string
          x-order: 2
```
@goforbroke1006
Copy link

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
Copy link
Contributor

renom commented 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
Copy link
Contributor

renom commented 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
Copy link
Contributor

renom commented 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
Copy link

The problem is relevant for me too. Are there any solutions?

@stijnderyckere
Copy link

This also frustrates me the most about the oapi codegen. Are there any workarounds for this?

@jamietanna
Copy link
Collaborator

jamietanna commented Jan 25, 2024

👀 #1190 should be merged today

@jamietanna
Copy link
Collaborator

jamietanna commented Jan 25, 2024

Closed by 3bf5755 (incorrectly attributed commit)

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

No branches or pull requests

9 participants