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

Skip entoas annotation causes generated code to not compile #251

Open
swalkerhppr opened this issue Apr 15, 2024 · 1 comment
Open

Skip entoas annotation causes generated code to not compile #251

swalkerhppr opened this issue Apr 15, 2024 · 1 comment

Comments

@swalkerhppr
Copy link

I'm not sure of your issue processes, so please let me know if I need to do something else/different.

Issue

Including the entoas.Skip(true) annotation on a field or edge causes the generated handlers to not be able to compile:

# entdemo/ent/ogent
ent/ogent/ogent.go:35:20: req.Password undefined (type *CreateUserReq has no field or method Password)
ent/ogent/ogent.go:109:18: req.Password undefined (type *UpdateUserReq has no field or method Password)
ent/ogent/responses.go:14:6: ret.Password undefined (type UserCreate has no field or method Password)
ent/ogent/responses.go:43:6: ret.Password undefined (type UserList has no field or method Password)
ent/ogent/responses.go:72:6: ret.Password undefined (type UserRead has no field or method Password)
ent/ogent/responses.go:101:6: ret.Password undefined (type UserUpdate has no field or method Password)
ent/ogent/responses.go:130:6: ret.Password undefined (type UserChildrenList has no field or method Password)
ent/ogent/responses.go:159:6: ret.Password undefined (type UserParentRead has no field or method Password)
ent/ogent/responses.go:188:6: ret.Password undefined (type UserSecretChildrenList has no field or method Password)
ent/ogent/responses.go:217:6: ret.Password undefined (type UserSecretParentRead has no field or method Password)
ent/ogent/responses.go:217:6: too many errors

Fix

I have a branch on my fork that fixes this issue for ogent: https://github.com/swalkerhppr/ogent/tree/skip-fix
It fixes skipping fields, but skipping edges makes the resulting ogen server not fully implemented:

./main.go:24:29: cannot use ogent.NewOgentHandler(client) (value of type *ogent.OgentHandler) as ogent.Handler value in argument to ogent.NewServer: *ogent.OgentHandler does not implement ogent.Handler (missing method ListUserSecretChildren)

Which I believe is an entoas issue as the openapi.json schema still has secret_parent and secret_children in it. Further research is needed for that.

Code that causes the issue

// ent/schema/user.go
package schema

import (
  "entgo.io/contrib/entoas"
  "entgo.io/ent"
  "entgo.io/ent/schema/edge"
  "entgo.io/ent/schema/field"
)

// User holds the schema definition for the User entity.
type User struct {
  ent.Schema
}

// Fields of the User.
func (User) Fields() []ent.Field {
  return []ent.Field{
    field.String("username"),
    field.String("password").
      Annotations(
        entoas.Skip(true), // Skip the "password" field
      ),
    }
}

// Edges of the User.
func (User) Edges() []ent.Edge {
  return []ent.Edge{
    edge.To("secret_children", User.Type).
      Annotations(
        entoas.Skip(true), // Skip the "secret_children" edge
      ).
      From("secret_parent").
      Unique().
      Annotations(
        entoas.Skip(true), // Skip the "secret_parent" edge
      ),
    edge.To("children", User.Type).
      From("parent").
      Unique(),
  }
}
// main.go
package main

import (
  "context"
  "log"
  "net/http"
  "ogent-test/ent"
  "ogent-test/ent/ogent"

  _ "github.com/mattn/go-sqlite3"
)

func main() {
    client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
    if err != nil {
        log.Fatalf("failed opening connection to sqlite: %v", err)
    }
    defer client.Close()

    if err := client.Schema.Create(context.Background()); err != nil {
        log.Fatalf("failed creating schema resources: %v", err)
    }

    srv, _ := ogent.NewServer(ogent.NewOgentHandler(client))

    log.Println("Starting server...")

    if err := http.ListenAndServe(":8080", srv); err != nil {
      log.Fatal(err)
    }
}

Expected behavior

  1. go generate ./...
  2. grep -ir "password\|secret" ent/ogent should show no references to password, secret_children nor secret_parent
  3. go run . should work compile/run
@swalkerhppr
Copy link
Author

Update: My suspicions were correct, entoas was generating endpoints for skipped edges. I fixed the issue there and made a pull request to contrib to fix the issue: ent/contrib#574

Using my updated versions produces the expected behavior. I think you'd want to wait until the entoas change is resolved until you'd want to update it here. I will create a pull request once/if the updates are in.

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

No branches or pull requests

1 participant