Skip to content

Commit

Permalink
Merge pull request #1269 from prgres/feat/marshal-ext
Browse files Browse the repository at this point in the history
feat: marshal pathItem and operation extentions
  • Loading branch information
tdakkota authored Jun 27, 2024
2 parents 2431639 + 4024c9f commit 01e42b7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
70 changes: 70 additions & 0 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -327,6 +328,41 @@ 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))
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.
Expand Down Expand Up @@ -381,6 +417,40 @@ 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))
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
}

// ExternalDocumentation describes a reference to external resource for extended documentation.
//
// See https://spec.openapis.org/oas/v3.1.0#external-documentation-object.
Expand Down
50 changes: 50 additions & 0 deletions spec_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ogen_test

import (
"encoding/json"
"reflect"
"testing"

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 01e42b7

Please sign in to comment.