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

Support for field.Strings/Ints #248

Open
dotwaffle opened this issue Sep 26, 2023 · 3 comments
Open

Support for field.Strings/Ints #248

dotwaffle opened this issue Sep 26, 2023 · 3 comments

Comments

@dotwaffle
Copy link

A schema I'm working with has a few ent fields of type Strings or Ints (an array in OAS). Right now, codegen baila with a confusing error that I've narrowed down to no support for these types.

Is that something you've just not got around to (in which case, shall I submit a PR once I have coded it) or is it a conscious decision for another reason?

Thanks in advance!

@matoszz
Copy link

matoszz commented Nov 8, 2023

I believe i'm impacted by a similar issue when trying to use field.JSON and specifying []strings{} in the field type. I've tried many permutations of annotations or other config but the failure message (super unhelpful) is:

running ent codegen: execute template "ogent/responses": template: response.tmpl:17:6: executing "ogent/responses" at <setFieldExpr $f $n "ret" "e">: error calling setFieldExpr: unexpected type: ""

I'm additionally confused why I'm unable to use ogen.String().AsArray() even though it's in the declaration for the field.JSON type: https://github.com/ent/contrib/blob/master/entoas/generator_test.go#L65

@jamesw201
Copy link

jamesw201 commented Jan 15, 2024

Agree with @matoszz, the only annotation that I've found will get field.JSON to work with entoas is something like:

type Model struct {
    ent.Schema
}

func (Model) Fields() []ent.Field {
	return []ent.Field{
    field.Int("id"),
    field.String("name"),
    field.JSON("location", []byte{}).
        Annotations(entoas.Schema(&ogen.Schema{
            Type:        "object",
            Description: "Location",
            Properties: []ogen.Property{
                {Name: "latitude", Schema: ogen.Double()},
                {Name: "longitude", Schema: ogen.Double()},
            },
        })),
    }
}

as described here: ent/ent#3573 (comment)

Whilst that will generate an openapi.json file, it then causes issues in ogent with:

ent/ogent/ogent.go:35:16: cannot use req.Location (variable of type CreateModelReqLocation) as []uint8 value in argument to b.SetLocation
ent/ogent/ogent.go:103:17: cannot use v (variable of type UpdateModelReqLocation) as []uint8 value in argument to b.SetLocation
ent/ogent/responses.go:14:17: cannot use e.Location (variable of type []uint8) as ModelCreateLocation value in assignment
ent/ogent/responses.go:43:17: cannot use e.Location (variable of type []uint8) as ModelListLocation value in assignment
ent/ogent/responses.go:72:17: cannot use e.Location (variable of type []uint8) as ModelReadLocation value in assignment
ent/ogent/responses.go:101:17: cannot use e.Location (variable of type []uint8) as ModelUpdateLocation value in assignment

Is there a way to have JSON.field describe a json object that entoas and ogent are happy with?

@Kirby980
Copy link

running ent codegen: execute template "ogent/responses": template: response.tmpl:17:6: executing "ogent/responses" at <setFieldExpr $f $n "ret" "e">: error calling setFieldExpr: unexpected type: "" Is this the reason to use field.JSON?

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

4 participants