Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort query parameters generated from object #681

Merged
merged 5 commits into from May 29, 2020

Conversation

breezewish
Copy link
Contributor

@breezewish breezewish commented Apr 20, 2020

Describe the PR

Currently swag may generate arbitrary order parameters when parameters come from query & object, like:

type Student struct {
	Name string
	Age int
	Teachers []string
	SkipField map[string]string
}

// @Param request query Student true "query params"

This results in different swagger spec being generated each time even if source code is not modified. Additionally, some openapi client generators like typescript-axios generates each query parameter as function parameters, like:

testGet(age?: number, name?: string, teachers?: Array<string>, options?: any): (axios?: AxiosInstance, basePath?: string) => AxiosPromise<void> {
    ...
}

Thus, unstable parameter order in the specification file will cause code failed to compile at all.

This PR makes parameter order stable by sorting them before output.

Relation issue

Additional context

@sdghchj
Copy link
Member

sdghchj commented Apr 22, 2020

Changes to be done.

@foxluqi
Copy link

foxluqi commented May 26, 2020

i think use order tag would be better than field name

@breezewish
Copy link
Contributor Author

@foxluqi Indeed! However currently the order of the tag is lost after being inserted to the hashmap.. I don't have ideas how to do it.

@codecov-commenter
Copy link

Codecov Report

Merging #681 into master will increase coverage by 0.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #681      +/-   ##
==========================================
+ Coverage   84.77%   84.85%   +0.08%     
==========================================
  Files           7        7              
  Lines        1859     1869      +10     
==========================================
+ Hits         1576     1586      +10     
  Misses        177      177              
  Partials      106      106              
Impacted Files Coverage Δ
operation.go 87.79% <100.00%> (+0.21%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 51088c1...c03e213. Read the comment docs.

@sdghchj sdghchj merged commit c7982e6 into swaggo:master May 29, 2020
@breezewish breezewish deleted the fixed-query-order branch May 29, 2020 07:06
@foxluqi
Copy link

foxluqi commented Jun 8, 2020

@foxluqi Indeed! However currently the order of the tag is lost after being inserted to the hashmap.. I don't have ideas how to do it.

i think the tag information can be read from schema.Properties?

@sdghchj
Copy link
Member

sdghchj commented Jun 8, 2020

@foxluqi @breeswish refer to go-openapi/spec#116 and go-openapi/spec#117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants