From 497702648b9e43afbd1f009f51fb05474b75f935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wi=C4=99cek?= Date: Tue, 25 Jun 2024 21:20:04 +0200 Subject: [PATCH 1/5] feat: marshal pathItem and operation extentions --- spec.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/spec.go b/spec.go index 82340cf1f..510faae4f 100644 --- a/spec.go +++ b/spec.go @@ -327,6 +327,40 @@ type PathItem struct { Common OpenAPICommon `json:"-" yaml:",inline"` } +func (s *PathItem) MarshalJSON() ([]byte, error) { + type Alias PathItem + originalJSON, err := json.Marshal(Alias(*s)) + if err != nil { + return nil, err + } + + d := jx.DecodeBytes(originalJSON) + e := jx.Encoder{} + + e.ObjStart() + if err := d.Obj(func(d *jx.Decoder, key string) error { + e.FieldStart(key) + raw, err := d.Raw() + if err != nil { + return err + } + + e.Raw(raw) + return nil + }); err != nil { + return nil, err + } + + for extK, extV := range s.Common.Extensions { + e.FieldStart(extK) + e.Str(extV.Value) + } + + e.ObjEnd() + + return e.Bytes(), nil +} + // Operation describes a single API operation on a path. // // See https://spec.openapis.org/oas/v3.1.0#operation-object. @@ -381,6 +415,40 @@ type Operation struct { Common OpenAPICommon `json:"-" yaml:",inline"` } +func (s *Operation) MarshalJSON() ([]byte, error) { + type Alias Operation + originalJSON, err := json.Marshal(Alias(*s)) + if err != nil { + return nil, err + } + + d := jx.DecodeBytes(originalJSON) + e := jx.Encoder{} + + e.ObjStart() + if err := d.Obj(func(d *jx.Decoder, key string) error { + e.FieldStart(key) + raw, err := d.Raw() + if err != nil { + return err + } + log.Info(string(raw)) + e.Raw(raw) + return nil + }); err != nil { + return nil, err + } + + for extK, extV := range s.Common.Extensions { + e.FieldStart(extK) + e.Str(extV.Value) + } + + e.ObjEnd() + + return e.Bytes(), nil +} + // ExternalDocumentation describes a reference to external resource for extended documentation. // // See https://spec.openapis.org/oas/v3.1.0#external-documentation-object. From 4f6430ccead00b85eef03e7d050eb0fce0d72cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wi=C4=99cek?= Date: Thu, 27 Jun 2024 18:48:27 +0200 Subject: [PATCH 2/5] fix: add missing jx import --- spec.go | 1 + 1 file changed, 1 insertion(+) diff --git a/spec.go b/spec.go index 510faae4f..7ba808399 100644 --- a/spec.go +++ b/spec.go @@ -3,6 +3,7 @@ package ogen import ( "encoding/json" + "github.com/go-faster/jx" "github.com/go-faster/yaml" "github.com/ogen-go/ogen/jsonschema" From eff5f34cbc83928237ca8c0262c60cf4b3a68998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wi=C4=99cek?= Date: Thu, 27 Jun 2024 18:48:35 +0200 Subject: [PATCH 3/5] chore: remove debug log --- spec.go | 1 - 1 file changed, 1 deletion(-) diff --git a/spec.go b/spec.go index 7ba808399..c150e644d 100644 --- a/spec.go +++ b/spec.go @@ -433,7 +433,6 @@ func (s *Operation) MarshalJSON() ([]byte, error) { if err != nil { return err } - log.Info(string(raw)) e.Raw(raw) return nil }); err != nil { From 44513cf99b516e05978e551ba63c1fc0407670fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wi=C4=99cek?= Date: Thu, 27 Jun 2024 19:11:17 +0200 Subject: [PATCH 4/5] test: add TestExtentionsMarshal --- spec_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/spec_test.go b/spec_test.go index 0646f7b56..5e0b19996 100644 --- a/spec_test.go +++ b/spec_test.go @@ -1,6 +1,7 @@ package ogen_test import ( + "encoding/json" "reflect" "testing" @@ -64,3 +65,52 @@ func TestSwaggerExtraction(t *testing.T) { a.Equal("2.0.0", s.Swagger) } } + +func TestExtensionsMarshal(t *testing.T) { + a := require.New(t) + + extensionValueFunc := func(key, value string) ogen.OpenAPICommon { + return ogen.OpenAPICommon{ + Extensions: ogen.Extensions{ + key: yaml.Node{Kind: yaml.ScalarNode, Value: value}, + }, + } + } + + extentionKey := "x-ogen-extension" + extenstionValue := "handler" + + { + pathItem := ogen.NewPathItem() + pathItem.Common = extensionValueFunc(extentionKey, extenstionValue) + + pathItemJson, err := json.Marshal(pathItem) + a.NoError(err) + + var output map[string]interface{} + err = json.Unmarshal(pathItemJson, &output) + a.NoError(err) + + v, ok := output[extentionKey] + a.True(ok) + + a.Equal(extenstionValue, v) + } + + { + op := ogen.NewOperation() + op.Common = extensionValueFunc(extentionKey, extenstionValue) + + pathItemJson, err := json.Marshal(op) + a.NoError(err) + + var output map[string]interface{} + err = json.Unmarshal(pathItemJson, &output) + a.NoError(err) + + v, ok := output[extentionKey] + a.True(ok) + + a.Equal(extenstionValue, v) + } +} From 4024c9ff20400854c5198827c960b79b62ef8126 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Thu, 27 Jun 2024 23:45:42 +0300 Subject: [PATCH 5/5] chore: fix linting issues --- spec.go | 2 ++ spec_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/spec.go b/spec.go index c150e644d..b7f80a8b8 100644 --- a/spec.go +++ b/spec.go @@ -328,6 +328,7 @@ type PathItem struct { Common OpenAPICommon `json:"-" yaml:",inline"` } +// MarshalJSON implements [json.Marshaler]. func (s *PathItem) MarshalJSON() ([]byte, error) { type Alias PathItem originalJSON, err := json.Marshal(Alias(*s)) @@ -416,6 +417,7 @@ type Operation struct { Common OpenAPICommon `json:"-" yaml:",inline"` } +// MarshalJSON implements [json.Marshaler]. func (s *Operation) MarshalJSON() ([]byte, error) { type Alias Operation originalJSON, err := json.Marshal(Alias(*s)) diff --git a/spec_test.go b/spec_test.go index 5e0b19996..29a2b8bda 100644 --- a/spec_test.go +++ b/spec_test.go @@ -84,11 +84,11 @@ func TestExtensionsMarshal(t *testing.T) { pathItem := ogen.NewPathItem() pathItem.Common = extensionValueFunc(extentionKey, extenstionValue) - pathItemJson, err := json.Marshal(pathItem) + pathItemJSON, err := json.Marshal(pathItem) a.NoError(err) var output map[string]interface{} - err = json.Unmarshal(pathItemJson, &output) + err = json.Unmarshal(pathItemJSON, &output) a.NoError(err) v, ok := output[extentionKey] @@ -101,11 +101,11 @@ func TestExtensionsMarshal(t *testing.T) { op := ogen.NewOperation() op.Common = extensionValueFunc(extentionKey, extenstionValue) - pathItemJson, err := json.Marshal(op) + pathItemJSON, err := json.Marshal(op) a.NoError(err) var output map[string]interface{} - err = json.Unmarshal(pathItemJson, &output) + err = json.Unmarshal(pathItemJSON, &output) a.NoError(err) v, ok := output[extentionKey]