kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

please user OrderedMap instead of the golang's map

Open ddzrc opened this issue 2 years ago • 11 comments

The order of properties after each parsing is inconsistent.

ddzrc avatar Oct 22 '22 01:10 ddzrc

Go maps iteration order is random, so I don't see why you mention deserialization?

Are you asking for this lib to use orderedmaps instead of maps so that operations can be deterministic? What's your use case? Do you see non-deterministic operations within the lib (that would be a bug)?

Also, what implementation of orderedmap would you recommend and why.

fenollp avatar Oct 23 '22 09:10 fenollp

https://github.com/iancoleman/orderedmap

zhangyongding avatar Nov 14 '22 06:11 zhangyongding

See also https://github.com/wk8/go-ordered-map v2 requires go18, which isn't a full year old https://go.dev/blog/go1.18

fenollp avatar Nov 30 '22 13:11 fenollp

We have noticed that not only is the ordering inconsistent, which can make it a mess for people trying to read the yaml files, but the output yaml will also break a swaggerhub display. By default, yaml is output in alphabetical order. This places 'components' before 'paths' and that will break some definitions. All the definitions are there, and it does validate in swaggerhub BUT the display does not show the definitions. We've seen this so far with 'allOf'

      schema:
        allOf:
          - $ref: '#/components/schemas/Guid'
          - title: Incident guid

If we manually move the 'components' section to be after 'paths', then all is ok again.

chippyash avatar Dec 05 '22 12:12 chippyash

Thanks for your feedback! We've got a couple github issues related to serialization order (and non-determinism must be annihilated) and I'm happy to hit two birds with one stone!

Note: solving this issue depends on https://github.com/wk8/go-ordered-map/pull/14#issuecomment-1336545760 if anyone want to help there ;)

fenollp avatar Dec 05 '22 13:12 fenollp

Note: solving this issue depends on wk8/go-ordered-map#14 (comment) if anyone want to help there ;)

Seems like this is already implemented on https://github.com/wk8/go-ordered-map/pull/16

codestation avatar Mar 19 '23 16:03 codestation

@chippyash Serialization order of fields is already adaptable. See openapi3.go and the MarshalJSON func implementation there. You should be able to reorder the fields there. I'll review your PR (provided you add a test for this ofc)

@codestation Look at my draft PR you'll see that's already being used :)

fenollp avatar Mar 19 '23 19:03 fenollp

FYI progress on this is blocked by https://github.com/getkin/kin-openapi/pull/763 If anyone wants to take a look at the failing tests there...

fenollp avatar Mar 19 '23 19:03 fenollp

@fenollp Any progress on this please?

chippyash avatar Apr 27 '23 10:04 chippyash

Hi @chippyash Not much progress to report. Still blocked on #763 Comments welcomed!

fenollp avatar May 13 '23 11:05 fenollp

The reason I'd like this: we generate our spec file and add it to git, and right now if we use MarshalJSON() as it is, the order of parameters etc changes every time the command is run.

(I'm not expecting anyone to magically fix this, just providing reason for having order preserving.)

ashb avatar Jan 15 '24 21:01 ashb