swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Any way to keep interface property in the same order

Open RoXuS opened this issue 3 years ago • 11 comments

Hey,

We commit the generated API code to follow change through our stories. It's good but the order of each interface change at each generation.

First generation:

interface A {
  b: string;
  c: string;
}

second generation:

interface A {
  c: string;
  b: string;
}

There is a way to keep order? Something like alphabetical order?

Thx.

RoXuS avatar Feb 12 '21 09:02 RoXuS

Hi @RoXuS
Do you use swagger-typescript-api via NodeJS generateApi() ?

js2me avatar Feb 12 '21 09:02 js2me

yes, we have a script inside our monorepo.

RoXuS avatar Feb 12 '21 09:02 RoXuS

I think it happens because of prettier use alphabetical order, you can override some prettier rules
image

js2me avatar Feb 12 '21 09:02 js2me

oh ok! Do you have an idea of what rules I can override?

RoXuS avatar Feb 12 '21 09:02 RoXuS

@RoXuS I have the same problem, currently I don't know why this happened :)
But I will try to fix that

js2me avatar Feb 12 '21 09:02 js2me

ohhh sorry misunderstanding, ok thx for your job!!

RoXuS avatar Feb 12 '21 09:02 RoXuS

@RoXuS Do you use tslint or eslint ?

js2me avatar Feb 12 '21 10:02 js2me

If tslint try to disable this rule object-literal-sort-keys

js2me avatar Feb 12 '21 10:02 js2me

If eslint try to disable this rule https://eslint.org/docs/rules/sort-keys#require-object-keys-to-be-sorted-sort-keys

js2me avatar Feb 12 '21 10:02 js2me

hum we use eslint but not on our "generated" folder?

RoXuS avatar Feb 13 '21 14:02 RoXuS

In my case keys were swapped randomly and this couldn't be linter/prettier issue, it was actually unstable OpenAPI output. The solution was to fetch and sort spec object before feeding it to generateApi. Could be a feature as it's useful.

bisubus avatar Sep 16 '21 21:09 bisubus