Skip to content

Commit e8c39e0

Browse files
committed
chore: add OgenExtentions type and fix testes
1 parent 8220aaf commit e8c39e0

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

entoas/annotation.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ type (
5050
// Skip specifies that the field will be ignored in spec.
5151
Skip bool
5252
// Extensions has map of OpenApi extenions
53-
Extensions ogen.Extensions
53+
Extensions OgenExtensions
5454
}
55+
OgenExtensions ogen.Extensions
5556
// OperationConfig holds meta information about a REST operation.
5657
OperationConfig struct {
5758
Policy Policy
5859
Groups serialization.Groups
59-
Extensions ogen.Extensions
60+
Extensions OgenExtensions
6061
}
6162
// OpenApiExtension holds meta information about OpenApi extension
6263
OpenApiExtension struct {
@@ -67,6 +68,10 @@ type (
6768
OperationConfigOption func(*OperationConfig)
6869
)
6970

71+
func (o OgenExtensions) Schema() ogen.Extensions {
72+
return ogen.Extensions(o)
73+
}
74+
7075
// Groups returns a OperationConfigOption that adds the given serialization groups to a OperationConfig.
7176
func Groups(gs ...string) Annotation {
7277
return Annotation{Groups: gs}
@@ -80,7 +85,7 @@ func OperationGroups(gs ...string) OperationConfigOption {
8085
func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption {
8186
return func(c *OperationConfig) {
8287
if c.Extensions == nil {
83-
c.Extensions = ogen.Extensions{}
88+
c.Extensions = OgenExtensions{}
8489
}
8590

8691
for _, e := range ext {
@@ -90,7 +95,7 @@ func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption {
9095
}
9196

9297
func Extensions(ext ...OpenApiExtension) Annotation {
93-
exts := ogen.Extensions{}
98+
exts := OgenExtensions{}
9499

95100
for _, e := range ext {
96101
exts[e.Name] = yaml.Node{Kind: yaml.ScalarNode, Value: e.Value}

entoas/annotation_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ func TestAnnotation(t *testing.T) {
3636
require.Equal(t, serialization.Groups{"create", "groups"}, a.Groups)
3737

3838
a = CreateOperation(OperationGroups("create", "groups"), OperationPolicy(PolicyExpose))
39-
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"create", "groups"}}, a.Create)
39+
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"create", "groups"}}, a.Create)
4040

4141
a = ReadOperation(OperationGroups("read", "groups"), OperationPolicy(PolicyExpose))
42-
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"read", "groups"}}, a.Read)
42+
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"read", "groups"}}, a.Read)
4343

4444
a = UpdateOperation(OperationGroups("update", "groups"), OperationPolicy(PolicyExpose))
45-
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"update", "groups"}}, a.Update)
45+
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"update", "groups"}}, a.Update)
4646

4747
a = DeleteOperation(OperationGroups("delete", "groups"), OperationPolicy(PolicyExpose))
48-
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"delete", "groups"}}, a.Delete)
48+
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"delete", "groups"}}, a.Delete)
4949

5050
a = ListOperation(OperationGroups("list", "groups"), OperationPolicy(PolicyExpose))
51-
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"list", "groups"}}, a.List)
51+
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"list", "groups"}}, a.List)
5252

5353
b := Example("example")
5454
require.Equal(t, "example", b.Example)
@@ -57,12 +57,18 @@ func TestAnnotation(t *testing.T) {
5757
require.Equal(t, ogen.Binary(), c.Schema)
5858

5959
a = a.Merge(b).(Annotation).Merge(c).(Annotation)
60+
// a.List.Extensions = ogen.Extensions{
61+
// "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"},
62+
// }
6063
ex := Annotation{
6164
Example: "example",
6265
Schema: ogen.Binary(),
6366
List: OperationConfig{
6467
Groups: serialization.Groups{"list", "groups"},
6568
Policy: PolicyExpose,
69+
// Extensions: ogen.Extensions{
70+
// "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"},
71+
// },
6672
},
6773
}
6874
require.Equal(t, ex, a)
@@ -89,6 +95,7 @@ func TestAnnotation(t *testing.T) {
8995
require.NotNil(t, ac)
9096
ac, err = SchemaAnnotation(&gen.Type{Annotations: gen.Annotations{a.Name(): ex}})
9197
require.NoError(t, err)
98+
9299
require.Equal(t, &ex, ac)
93100

94101
ac, err = FieldAnnotation(new(gen.Field))

entoas/generator.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func paths(g *gen.Graph, spec *ogen.Spec) error {
201201
return err
202202
}
203203

204-
path(spec, root).Common.Extensions = ant.Extensions
204+
path(spec, root).Common.Extensions = ant.Extensions.Schema()
205205

206206
// Create operation.
207207
if contains(ops, OpCreate) {
@@ -307,7 +307,7 @@ func createOp(spec *ogen.Spec, n *gen.Type, allowClientUUIDs bool) (*ogen.Operat
307307
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
308308
return nil, err
309309
}
310-
op.Common.Extensions = ant.Create.Extensions
310+
op.Common.Extensions = ant.Create.Extensions.Schema()
311311

312312
return op, nil
313313
}
@@ -345,7 +345,7 @@ func readOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
345345
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
346346
return nil, err
347347
}
348-
op.Common.Extensions = ant.Read.Extensions
348+
op.Common.Extensions = ant.Read.Extensions.Schema()
349349

350350
return op, nil
351351
}
@@ -422,7 +422,7 @@ func updateOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
422422
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
423423
return nil, err
424424
}
425-
op.Common.Extensions = ant.Update.Extensions
425+
op.Common.Extensions = ant.Update.Extensions.Schema()
426426

427427
return op, nil
428428
}
@@ -455,7 +455,7 @@ func deleteOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
455455
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
456456
return nil, err
457457
}
458-
op.Common.Extensions = ant.Delete.Extensions
458+
op.Common.Extensions = ant.Delete.Extensions.Schema()
459459

460460
return op, nil
461461
}
@@ -507,7 +507,7 @@ func listOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
507507
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
508508
return nil, err
509509
}
510-
op.Common.Extensions = ant.List.Extensions
510+
op.Common.Extensions = ant.List.Extensions.Schema()
511511

512512
return op, nil
513513
}

0 commit comments

Comments
 (0)