swaggo / swag

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

feat: Add x-enum-descriptions to generated Swagger documentation for Enum #1878

Closed wakamenod closed 1 month ago

wakamenod commented 2 months ago

This pull request introduces the x-enum-descriptions extension to the generated Swagger documentation when working with enums. The new extension provides a list of descriptions for enum values, aligned with the x-enum-varnames.

Details of the changes:

A new constant enumDescriptionsExtension has been added to support the x-enum-descriptions extension. The ParseDefinition function has been updated to create a x-enum-descriptions list based on the order of x-enum-varnames and corresponding enum comments.

The x-enum-comments and x-enum-varnames are now matched in order, and their descriptions are output in the x-enum-descriptionfs array. Test cases have been updated to reflect the addition of x-enum-descriptions, demonstrating the new output in the expected JSON.

Why this change is necessary:

The addition of x-enum-descriptions enhances the clarity and usability of Swagger documentation by ensuring that descriptions for enum values are explicitly defined and correctly ordered. This can be particularly useful for clients and developers who rely on clear enum definitions and descriptions in API documentation.

Testing:

Updated relevant test cases in testdata/enums/expected.json to include x-enum-descriptions. Verified that the descriptions are correctly aligned with x-enum-varnames and appear in the expected order. Impact:

This change does not affect existing functionality but adds a new optional extension to the generated documentation. No breaking changes are introduced, and the update is backward compatible. Please review the changes and let me know if there are any adjustments or additional tests needed. Your feedback is appreciated!

ubogdan commented 1 month ago

@wakamenod Thanks for your contribution!