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

fix(entoas): example annotation #586

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

prgres
Copy link

@prgres prgres commented Jun 17, 2024

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:

func Example(v interface{}) Annotation { return Annotation{Example: v} }

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:

field.String("name").
	Annotations(entoas.Example("zxc")),

In very different scenarios - on fields, edges, and top-level annotations nothing changed. The spec still looks like this

{
  "summary": "Create a new User",
  "description": "Creates a new User and persists it to storage.",
  "operationId": "createUser",
  "requestBody": {
    "description": "User to create",
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
            "properties": {
              "name": {
                "type": "string"
              }
           } 
         }
       }
     }
   }
 }

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:

if ant.Example != nil {
	jv, err := json.Marshal(ant.Example)
	if err != nil {
		return nil, fmt.Errorf("cannot marshal example annotation for field %s", f.Name)
	}
	schema.Example = jv
}

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 statement

(func types(t string) *ogen.Schema {
	switch t {
	case "bool":
		return ogen.Bool()
	case "time.Time":
		return ogen.DateTime()
	case "string":
		return ogen.String()
	case "[]byte":
		return ogen.Bytes()
	case "uuid.UUID":
		return ogen.UUID()
        // ....

For the sake of being future-proof, I also added a unit test that covers that case (func TestOgenSchema_Example(t *testing.T)

@prgres
Copy link
Author

prgres commented Jun 28, 2024

@a8m @masseelch any updates on this? just a simple feature

prgres added a commit to prgres/ent-contrib that referenced this pull request Jun 30, 2024
@STommydx
Copy link

I am trying to add examples with entoas.Example() annotation and found it doesn't work at all. I stumbled across this pull request and #577 that sadly have no updates from the maintainers. Thank you for your contribution. Hope this can be merged soon.

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.

2 participants