fix: use x-example for non-body parameters in Swagger 2.0#2138
Open
22skyy wants to merge 1 commit intoswaggo:masterfrom
Open
fix: use x-example for non-body parameters in Swagger 2.0#213822skyy wants to merge 1 commit intoswaggo:masterfrom
22skyy wants to merge 1 commit intoswaggo:masterfrom
Conversation
Swagger 2.0 specification does not support 'example' field for query/path/header/form parameters (Parameter Object). The 'example' field is only valid in Schema Object (used for body parameters). This change uses the 'x-example' vendor extension for non-body parameters, which is the recommended workaround for Swagger 2.0. Body parameters continue to use 'example' as before since they use Schema Object where 'example' is valid. References: - https://swagger.io/specification/v2/#parameter-object - https://swagger.io/docs/specification/v2_0/swagger-extensions/ Fixes swaggo#2137
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix non-compliant Swagger 2.0 output when using
example()attribute on non-body parameters.Problem
The
examplefield is not part of the Swagger 2.0 Parameter Object specification for query/path/header/form parameters. It is only valid in Schema Object (used for body parameters).This causes validation errors in downstream tools like OpenAPI Generator:
Solution
For non-body parameters, use the
x-examplevendor extension instead ofexample. This is the recommended workaround for Swagger 2.0.Body parameters continue to use
exampleas before, since they use Schema Object whereexampleis valid.Before
After
Changes
operation.go: ModifiedsetExample()to use vendor extension for non-body parametersoperation_test.go: Updated existing tests and added new test casesBreaking Changes
Non-body parameters will now output
x-exampleinstead ofexample. This is technically a breaking change for tools that specifically look forexampleon non-body parameters, but it fixes spec compliance.References