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
I found an issue with ent and entoas. While checking out a generated
openapi.json
file I thought it would be nice to have some example values prepared for testing the API.Description
By browsing the entoas's source code, I found annotations for that [entoas.Example] ready to use:
contrib/entoas/annotation.go
Line 75 in d262910
After playing with it for some time, even if it provided examples, it seems that is completely ignored.
Current behaviour
I was adding them like this:
In very different scenarios - on fields, edges, and top-level annotations nothing changed. The spec still looks like this
Root cause
After, some debugging, I found out that there is no code responsible for that. So here I am.
The main problem is in the
OgenSchema
function. It gets a schema for each field type (f.Type.String()
) and returns it without any changes - e.g."string"
->ogen.String()
. Anything else is basically ignored.Fix & changes
After changing if-logic to switch-logic in the
generator.go
file it only required adding a quick fix:But it came out there was another problem underlying with the
types
maps. In the upstream, it holds pointers to ogen's schemas so any change which is done on the schema is being propagated to the other fields. My solution for this is a function with a switch statementFor the sake of being future-proof, I also added a unit test that covers that case (
func TestOgenSchema_Example(t *testing.T)