wI2L / fizz

:lemon: Gin wrapper with OpenAPI 3 spec generation
https://pkg.go.dev/github.com/wI2L/fizz
MIT License
214 stars 52 forks source link

feat: support providing examples for pointer fields #56

Closed nikicc closed 3 years ago

nikicc commented 3 years ago

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[bot] commented 3 years ago

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%) :arrow_up:
openapi/types.go 100.00% <100.00%> (+2.81%) :arrow_up:

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.

wI2L commented 3 years ago

@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.

nikicc commented 3 years ago

@wI2L great, will check.

wI2L commented 3 years ago

I am looking into it to add more tests cases.

nikicc commented 3 years ago

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