Skip to content

Option to generate a struct without sorting fields #458

Closed

Description

@mf-sakura

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"`
}

Activity

  1. weusego commented on Mar 25, 2022

    @weusego

    +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.

  2. gnvk commented on Jun 2, 2022

    @gnvk

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

  3. goforbroke1006 commented on Jun 15, 2022

    @goforbroke1006

    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?

  4. renom commented on Sep 18, 2023

    @renom
    Contributor

    Well, it's somewhere inside convertToJSONableObject in invopop/yaml because go-yaml/yaml keeps the ordering when YAML is converted to map.

  5. renom commented on Oct 19, 2023

    @renom
    Contributor

    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)

  6. DeOne4eg commented on Dec 17, 2023

    @DeOne4eg

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

  7. stijnderyckere commented on Jan 18, 2024

    @stijnderyckere

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

  8. jamietanna commented on Jan 25, 2024

    @jamietanna
    Member

    👀 #1190 should be merged today

  9. jamietanna commented on Jan 25, 2024

    @jamietanna
    Member

    Closed by 3bf5755 (incorrectly attributed commit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions