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

Add sortFields = true|false to disable sorting when generating the model #1737

Closed
nimo23 opened this issue Dec 5, 2024 · 4 comments · Fixed by #1763
Closed

Add sortFields = true|false to disable sorting when generating the model #1737

nimo23 opened this issue Dec 5, 2024 · 4 comments · Fixed by #1763
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@nimo23
Copy link

nimo23 commented Dec 5, 2024

Orval generates *.ts model files from a given openapi.yml. However, the fields within the *.ts model interface/type are always sorted implicitly instead of using the field order defined in openapi.yml. For example:

What are the steps to reproduce this issue?

1.openapi.yml has a User endpoint with the following

openapi: 3.1.0
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
        username:
          type: string
        password:
          type: string
  1. let orval generate the user.ts and look at the ordering of the fields:
// overal sorts the fields in alphabetical order
export interface User {
  id?: string;
  password?: string;
  username?: string;
}

Please add a configuration to https://orval.dev/reference/configuration/output, e.g. sortFields = true|false. If set to false, the field order should match the order defined in openapi.yml. In the above example, it should be:

// sortFields=false, i.e. overal uses the sorting order as defined in openapi.yml
export interface User {
  id?: string;
  username?: string;
  password?: string;
}

What versions are you using?

Orval v 7.3.0

@melloware melloware added enhancement New feature or request good first issue Good for newcomers labels Dec 5, 2024
@melloware
Copy link
Collaborator

Similar to this issue too: #1382 I think in general everything should be sorted alpha if possible and this new flag suggested by @nimo23 would basically ignore all sorting...

@nimo23
Copy link
Author

nimo23 commented Dec 5, 2024

I think in general everything should be sorted alpha if possible

I think Orval should not deviate from the sorting specified in openapi.yml unless the user explicitly sets it via sortFields=true., i.e. the specified sorting from openapi.yml should be used by default.

@melloware
Copy link
Collaborator

PR is welcome...

@nimo23
Copy link
Author

nimo23 commented Dec 7, 2024

I have looked at the code and am trying to figure out which steps are necessary and which files need to be adjusted:

  1. add sortFields to either OverrideOutput or OutputOptions in packages.core.src.generators.types.ts
  2. where to put it in packages.core.src.generators.options.ts
  3. where is the current (alphabetical) sorting of the (generated) properties ? Anywhere in packages.core.src.writers ?
  4. Add test case
  5. Add documentation in https://github.com/orval-labs/orval/blob/master/docs/src/pages/reference/configuration/output.md

I think someone who knows the code in detail can do it faster than the explanation :)

Btw, maybe there is already a setting in https://github.com/OpenAPITools/openapi-generator (which orval uses (or can use) under the hood)?

@melloware melloware added this to the 7.3.1 milestone Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants