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

feat: support providing examples for pointer fields #56

Merged
merged 3 commits into from
May 7, 2021

Conversation

nikicc
Copy link
Contributor

@nikicc nikicc commented May 5, 2021

The issue

Currently, the example tag will not work for fields that are pointers to other types. For example, for this struct:

type Data struct {
	A string `example:"valA"`
	B *string `example:"valB"`
}

the valA will be present in the generated spec, while valB will not.

Reasoning

As having pointers to other types is the simplest solution to get null values in the JSON responses for missing data (instead of the Go defaults for the corresponding type), I think the example tag should also work for pointers.

Proposed solution

When parsing the example tag values, if we get a pointer, try to parse the value that pointer points to.

@codecov
Copy link

codecov bot commented May 5, 2021

Codecov Report

Merging #56 (ab1b50d) into master (6c2edb3) will increase coverage by 0.55%.
The diff coverage is 85.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #56      +/-   ##
==========================================
+ Coverage   95.33%   95.88%   +0.55%     
==========================================
  Files           7        7              
  Lines         900      924      +24     
==========================================
+ Hits          858      886      +28     
+ Misses         27       22       -5     
- Partials       15       16       +1     
Impacted Files Coverage Δ
openapi/generator.go 94.39% <84.61%> (+0.54%) ⬆️
openapi/types.go 100.00% <100.00%> (+2.81%) ⬆️

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 6c2edb3...ab1b50d. Read the comment docs.

@nikicc nikicc marked this pull request as ready for review May 5, 2021 10:30
@wI2L wI2L force-pushed the support-examples-for-pointers branch from 2a0bec3 to 27813d4 Compare May 5, 2021 14:25
@wI2L
Copy link
Owner

wI2L commented May 5, 2021

@nikicc
Thanks for the PR, I'm in favor of that fix.

However, I noticed that it was lacking support for pointer-chain fields, such as **bool or ***string...
I hijacked your PR and pushed a new commit to fix the current codebase where that support was missing, and added new tests.
(I fixed some linting issues, unrelated to that current patch, don't mind it please, I wasn't keen to create another PR just for those changes, so I included them while I can).

Please, don't hesitate to comment and/or give a feddback on my changes.

@wI2L wI2L requested a review from rbeuque74 May 5, 2021 14:30
@nikicc
Copy link
Contributor Author

nikicc commented May 5, 2021

@wI2L great, will check.

Copy link
Contributor Author

@nikicc nikicc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wI2L other than the coverage complaining, LGTM 👍

@wI2L
Copy link
Owner

wI2L commented May 5, 2021

I am looking into it to add more tests cases.

@nikicc
Copy link
Contributor Author

nikicc commented May 7, 2021

@wI2L as far as I'm concerned, this can go in. Do you have something else you want to add or can we merge?

@wI2L wI2L changed the title Support providing examples for pointer feat: support providing examples for pointer fields May 7, 2021
@wI2L wI2L merged commit d0b235e into wI2L:master May 7, 2021
@nikicc nikicc deleted the support-examples-for-pointers branch May 7, 2021 12:28
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.

3 participants