diff --git a/entoas/annotation.go b/entoas/annotation.go index de97f134a..1865afcb8 100644 --- a/entoas/annotation.go +++ b/entoas/annotation.go @@ -50,13 +50,14 @@ type ( // Skip specifies that the field will be ignored in spec. Skip bool // Extensions has map of OpenApi extenions - Extensions ogen.Extensions + Extensions OgenExtensions } + OgenExtensions ogen.Extensions // OperationConfig holds meta information about a REST operation. OperationConfig struct { Policy Policy Groups serialization.Groups - Extensions ogen.Extensions + Extensions OgenExtensions } // OpenApiExtension holds meta information about OpenApi extension OpenApiExtension struct { @@ -67,6 +68,10 @@ type ( OperationConfigOption func(*OperationConfig) ) +func (o OgenExtensions) Schema() ogen.Extensions { + return ogen.Extensions(o) +} + // Groups returns a OperationConfigOption that adds the given serialization groups to a OperationConfig. func Groups(gs ...string) Annotation { return Annotation{Groups: gs} @@ -80,7 +85,7 @@ func OperationGroups(gs ...string) OperationConfigOption { func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption { return func(c *OperationConfig) { if c.Extensions == nil { - c.Extensions = ogen.Extensions{} + c.Extensions = OgenExtensions{} } for _, e := range ext { @@ -90,7 +95,7 @@ func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption { } func Extensions(ext ...OpenApiExtension) Annotation { - exts := ogen.Extensions{} + exts := OgenExtensions{} for _, e := range ext { exts[e.Name] = yaml.Node{Kind: yaml.ScalarNode, Value: e.Value} diff --git a/entoas/annotation_test.go b/entoas/annotation_test.go index f4a62dcf3..daa5fbfaf 100644 --- a/entoas/annotation_test.go +++ b/entoas/annotation_test.go @@ -36,19 +36,19 @@ func TestAnnotation(t *testing.T) { require.Equal(t, serialization.Groups{"create", "groups"}, a.Groups) a = CreateOperation(OperationGroups("create", "groups"), OperationPolicy(PolicyExpose)) - require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"create", "groups"}}, a.Create) + require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"create", "groups"}}, a.Create) a = ReadOperation(OperationGroups("read", "groups"), OperationPolicy(PolicyExpose)) - require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"read", "groups"}}, a.Read) + require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"read", "groups"}}, a.Read) a = UpdateOperation(OperationGroups("update", "groups"), OperationPolicy(PolicyExpose)) - require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"update", "groups"}}, a.Update) + require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"update", "groups"}}, a.Update) a = DeleteOperation(OperationGroups("delete", "groups"), OperationPolicy(PolicyExpose)) - require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"delete", "groups"}}, a.Delete) + require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"delete", "groups"}}, a.Delete) a = ListOperation(OperationGroups("list", "groups"), OperationPolicy(PolicyExpose)) - require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"list", "groups"}}, a.List) + require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"list", "groups"}}, a.List) b := Example("example") require.Equal(t, "example", b.Example) @@ -57,12 +57,18 @@ func TestAnnotation(t *testing.T) { require.Equal(t, ogen.Binary(), c.Schema) a = a.Merge(b).(Annotation).Merge(c).(Annotation) + // a.List.Extensions = ogen.Extensions{ + // "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"}, + // } ex := Annotation{ Example: "example", Schema: ogen.Binary(), List: OperationConfig{ Groups: serialization.Groups{"list", "groups"}, Policy: PolicyExpose, + // Extensions: ogen.Extensions{ + // "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"}, + // }, }, } require.Equal(t, ex, a) @@ -89,6 +95,7 @@ func TestAnnotation(t *testing.T) { require.NotNil(t, ac) ac, err = SchemaAnnotation(&gen.Type{Annotations: gen.Annotations{a.Name(): ex}}) require.NoError(t, err) + require.Equal(t, &ex, ac) ac, err = FieldAnnotation(new(gen.Field)) diff --git a/entoas/generator.go b/entoas/generator.go index 6c6afba0d..220e95288 100644 --- a/entoas/generator.go +++ b/entoas/generator.go @@ -201,7 +201,7 @@ func paths(g *gen.Graph, spec *ogen.Spec) error { return err } - path(spec, root).Common.Extensions = ant.Extensions + path(spec, root).Common.Extensions = ant.Extensions.Schema() // Create operation. if contains(ops, OpCreate) { @@ -307,7 +307,7 @@ func createOp(spec *ogen.Spec, n *gen.Type, allowClientUUIDs bool) (*ogen.Operat if err := ant.Decode(n.Annotations[ant.Name()]); err != nil { return nil, err } - op.Common.Extensions = ant.Create.Extensions + op.Common.Extensions = ant.Create.Extensions.Schema() return op, nil } @@ -345,7 +345,7 @@ func readOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) { if err := ant.Decode(n.Annotations[ant.Name()]); err != nil { return nil, err } - op.Common.Extensions = ant.Read.Extensions + op.Common.Extensions = ant.Read.Extensions.Schema() return op, nil } @@ -422,7 +422,7 @@ func updateOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) { if err := ant.Decode(n.Annotations[ant.Name()]); err != nil { return nil, err } - op.Common.Extensions = ant.Update.Extensions + op.Common.Extensions = ant.Update.Extensions.Schema() return op, nil } @@ -455,7 +455,7 @@ func deleteOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) { if err := ant.Decode(n.Annotations[ant.Name()]); err != nil { return nil, err } - op.Common.Extensions = ant.Delete.Extensions + op.Common.Extensions = ant.Delete.Extensions.Schema() return op, nil } @@ -507,7 +507,7 @@ func listOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) { if err := ant.Decode(n.Annotations[ant.Name()]); err != nil { return nil, err } - op.Common.Extensions = ant.List.Extensions + op.Common.Extensions = ant.List.Extensions.Schema() return op, nil }