swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.46k stars 1.19k forks source link

`swag init` generates `swagger.yaml` file in reverse order #1765

Open itaranto opened 6 months ago

itaranto commented 6 months ago

Describe the bug

swag init generates the swagger.yaml file in reverse order. The swagger.json file of the same spec is in correct order.

To Reproduce

Steps to reproduce the behavior:

  1. Run the following:
    swag init \
    --generalInfo "${some_dir}/router.go" \
    --output "${some_dir}i/docs" \
    --outputTypes json,yaml
  2. Go to "${some_dir}" and open the swagger.yaml and swagger.json files
  3. See the contents of both files

Expected behavior Both swagger.yaml and swagger.json files are in correct order.

Screenshots swagger.yaml file in reverse order but swagger.json it's not.

For example, for the YAML file the description is last, for the JSON file the description is first and so on.

Your swag version e.g. 1.16.3

Your go version e.g. 1.21.6

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

sdghchj commented 6 months ago

yaml file is converted from json and will lose orders as a result of using map[string]interface{} during json decoding. if we marshal spec.Swagger directly into yaml, the generated file may be huge with many useless lines, because spec.Swagger haven't set omitempty for yaml encoding.

go-openapi/spec has not considered to marshal into yaml, I think, refer to https://github.com/go-openapi/spec/blob/master/swagger.go

itaranto commented 6 months ago

yaml file is converted from json and will lose orders as a result of using map[string]interface{} during json decoding. if we marshal spec.Swagger directly into yaml, the generated file may be huge with many useless lines, because spec.Swagger haven't set omitempty for yaml encoding.

go-openapi/spec has not considered to marshal into yaml, I think, refer to https://github.com/go-openapi/spec/blob/master/swagger.go

OK, so I guess this could be improved by implementing a YAML marshaller in go-openapi.

As I imagine, that's no small task. I'll consider looking at the code and see if I can do a PR there.