Swagger API Documentation: Maintaining Custom Property Order

In Go, how do I keep the properties in the order I add them for Swagger? Can Swagger work with an ordered list instead of an unordered map?

type ApiOutput struct {
    Label      string         `json:"label"`
    Attributes []ApiElement   `json:"attributes"`
}

type ApiElement struct {
    DataType  string   `json:"data_type"`
    DataForm  string   `json:"data_form"`
    Choices   []string `json:"choices"`
}

hey, try writing a custom marshaller to encode your props using a slice. swagger natively support unordered maps so you need to manually enforce the order using extra code. good luck!

In my experience, maintaining a defined property order for Swagger documentation often means stepping outside the default behavior offered by the toolchain. A viable solution involved creating an intermediary structure that explicitly arranges the keys. I built a function that constructs the ordered list of properties before passing it to Swagger, thereby ensuring the desired order in the output documentation. Although it requires extra effort and careful integration with your existing code, this method provided the control necessary to meet strict ordering requirements.

Based on my experience, one effective workaround was to change the way the documentation is generated. Instead of relying solely on Swagger’s default behavior, I considered manually constructing the JSON output for the API. I developed a helper function that iterates over a predefined list of keys, pulling corresponding values from the struct instance. This approach provided me with complete control over key ordering. Although it did require some additional code to handle the serialization process, the result was a Swagger document that exactly matched my intended layout.

hey, i managed it by creating a secondary slice to define key order and then custom-marshal it. not the most elegant, but it worked to enforce the order and kept my swagger doc as i wanted. give it a try if u need ordered output.