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

v0.16.6 #291

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package base

import (
"encoding/json"
"github.com/pb33f/libopenapi/datamodel/high"
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base"
Expand Down Expand Up @@ -492,6 +493,29 @@ func (s *Schema) MarshalYAML() (interface{}, error) {
return nb.Render(), nil
}

// MarshalJSON will create a ready to render JSON representation of the Schema object.
func (s *Schema) MarshalJSON() ([]byte, error) {
nb := high.NewNodeBuilder(s, s.low)

// determine index version
idx := s.GoLow().Index
if idx != nil {
if idx.GetConfig().SpecInfo != nil {
nb.Version = idx.GetConfig().SpecInfo.VersionNumeric
}
}
// render node
node := nb.Render()
var renderedJSON map[string]interface{}

// marshal into struct
_ = node.Decode(&renderedJSON)

// return JSON bytes
return json.Marshal(renderedJSON)
}

// MarshalYAMLInline will render out the Schema pointer as YAML, and all refs will be inlined fully
func (s *Schema) MarshalYAMLInline() (interface{}, error) {
nb := high.NewNodeBuilder(s, s.low)
nb.Resolve = true
Expand All @@ -504,3 +528,25 @@ func (s *Schema) MarshalYAMLInline() (interface{}, error) {
}
return nb.Render(), nil
}

// MarshalJSONInline will render out the Schema pointer as JSON, and all refs will be inlined fully
func (s *Schema) MarshalJSONInline() ([]byte, error) {
nb := high.NewNodeBuilder(s, s.low)
nb.Resolve = true
// determine index version
idx := s.GoLow().Index
if idx != nil {
if idx.GetConfig().SpecInfo != nil {
nb.Version = idx.GetConfig().SpecInfo.VersionNumeric
}
}
// render node
node := nb.Render()
var renderedJSON map[string]interface{}

// marshal into struct
_ = node.Decode(&renderedJSON)

// return JSON bytes
return json.Marshal(renderedJSON)
}
4 changes: 2 additions & 2 deletions datamodel/high/base/schema_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (sp *SchemaProxy) Render() ([]byte, error) {
return yaml.Marshal(sp)
}

// MarshalYAML will create a ready to render YAML representation of the ExternalDoc object.
// MarshalYAML will create a ready to render YAML representation of the SchemaProxy object.
func (sp *SchemaProxy) MarshalYAML() (interface{}, error) {
var s *Schema
var err error
Expand All @@ -203,7 +203,7 @@ func (sp *SchemaProxy) MarshalYAML() (interface{}, error) {
}
}

// MarshalYAMLInline will create a ready to render YAML representation of the ExternalDoc object. The
// MarshalYAMLInline will create a ready to render YAML representation of the SchemaProxy object. The
// $ref values will be inlined instead of kept as is.
func (sp *SchemaProxy) MarshalYAMLInline() (interface{}, error) {
var s *Schema
Expand Down
76 changes: 76 additions & 0 deletions datamodel/high/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,82 @@ allOf:
assert.Equal(t, testSpec, string(schemaBytes))
}

func TestNewSchemaProxy_RenderSchema_JSON(t *testing.T) {
testSpec := `type: object
description: something object
`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)

// add a config
idxConfig := index.CreateOpenAPIIndexConfig()
idxConfig.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3,
}
idx := index.NewSpecIndexWithConfig(nil, idxConfig)

err := sp.Build(context.Background(), nil, compNode.Content[0], idx)
assert.NoError(t, err)

lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

assert.Equal(t, schemaProxy, compiled.ParentProxy)

assert.NotNil(t, compiled)
assert.Nil(t, schemaProxy.GetBuildError())

// now render it out, it should be identical, but in JSON
schemaBytes, _ := compiled.MarshalJSON()
assert.Equal(t, `{"description":"something object","type":"object"}`, string(schemaBytes))
}

func TestNewSchemaProxy_RenderSchema_JSONInline(t *testing.T) {
testSpec := `type: object
description: something object
`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)

// add a config
idxConfig := index.CreateOpenAPIIndexConfig()
idxConfig.SpecInfo = &datamodel.SpecInfo{
VersionNumeric: 3,
}
idx := index.NewSpecIndexWithConfig(nil, idxConfig)

err := sp.Build(context.Background(), nil, compNode.Content[0], idx)
assert.NoError(t, err)

lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

assert.Equal(t, schemaProxy, compiled.ParentProxy)

assert.NotNil(t, compiled)
assert.Nil(t, schemaProxy.GetBuildError())

// now render it out, it should be identical, but in JSON
schemaBytes, _ := compiled.MarshalJSONInline()
assert.Equal(t, `{"description":"something object","type":"object"}`, string(schemaBytes))
}

func TestNewSchemaProxy_RenderSchemaWithMultipleObjectTypes(t *testing.T) {
testSpec := `type: object
description: something object
Expand Down
Loading