Skip to content
Open
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
97 changes: 97 additions & 0 deletions bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3347,3 +3347,100 @@ paths:
require.NoError(t, err)
require.NotNil(t, bundledBytes)
}

func TestBundleBytes_SiblingRefUnevaluatedKeywordsStayBesideAllOf(t *testing.T) {
tmpDir := t.TempDir()

rootSpec := `openapi: 3.1.0
info: {title: bundle, version: '1'}
paths:
/x:
post:
requestBody:
content:
application/json:
schema:
$ref: 'ext.yaml#/components/schemas/Base'
required: [a]
unevaluatedProperties: false
responses:
'200':
description: ok
components:
schemas:
Base:
$id: 'https://example.com/schemas/base'
type: object
Scoped:
$ref: 'base'
$id: 'https://example.com/schemas/scoped'
$defs:
Node:
type: object
properties:
next:
$ref: '#/$defs/Node'
unevaluatedProperties:
$ref: '#/$defs/Node'
`
extSpec := `components:
schemas:
Base:
type: object
properties:
a:
type: integer
`
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "root.yaml"), []byte(rootSpec), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "ext.yaml"), []byte(extSpec), 0o644))

cfg := datamodel.NewDocumentConfiguration()
cfg.BasePath = tmpDir
cfg.SpecFilePath = "root.yaml"
cfg.AllowFileReferences = true
bundled, err := BundleBytes([]byte(rootSpec), cfg)
require.NoError(t, err)

subtree := func(path ...string) string {
t.Helper()
node := lookupMappingNode(t, bundled, path...)
require.NotNil(t, node, strings.Join(path, "."))
out, err := yaml.Marshal(node)
require.NoError(t, err)
return string(out)
}

// unevaluatedProperties only sees properties evaluated by adjacent keywords, so it must stay
// beside the allOf; inside an allOf member it would not see what the $ref target evaluated.
assert.Equal(t, `allOf:
- required:
- a
- type: object
properties:
a:
type: integer
unevaluatedProperties: false
`, subtree("paths", "/x", "post", "requestBody", "content", "application/json", "schema"))

// $id and $defs stay beside it too: 'base' resolves against Scoped's $id to the local Base,
// and '#/$defs/Node' resolves against Scoped's $id.
assert.Equal(t, `allOf:
- $id: 'https://example.com/schemas/base'
type: object
$id: 'https://example.com/schemas/scoped'
$defs:
Node:
type: object
properties:
next:
type: object
properties:
next:
$ref: '#/$defs/Node'
unevaluatedProperties:
type: object
properties:
next:
$ref: '#/$defs/Node'
`, subtree("components", "schemas", "Scoped"))
}
38 changes: 22 additions & 16 deletions datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,16 +540,17 @@ func (s *Schema) RenderInline() ([]byte, error) {

// MarshalYAML will create a ready to render YAML representation of the Schema object.
func (s *Schema) MarshalYAML() (interface{}, error) {
if node := s.booleanSchemaNode(); node != nil {
return node, nil
}

// the sibling view of a $ref with siblings renders like its proxy, even when it is boolean.
if s.ParentProxy != nil {
if node, ok, err := s.ParentProxy.renderTransformedRefWithSiblings(s); ok || err != nil {
return node, err
}
}

if node := s.booleanSchemaNode(); node != nil {
return node, nil
}

nb := high.NewNodeBuilder(s, s.low)

// determine index version
Expand All @@ -564,16 +565,19 @@ func (s *Schema) MarshalYAML() (interface{}, error) {

// MarshalJSON will create a ready to render JSON representation of the Schema object.
func (s *Schema) MarshalJSON() ([]byte, error) {
if s.BooleanSchema != nil {
return json.Marshal(*s.BooleanSchema)
}

if s.ParentProxy != nil && s.ParentProxy.isParsedRefWithSiblings() {
node, err := s.ParentProxy.referenceYAMLNodeForSchema(s)
// like MarshalYAML, render the full schema when the $ref form cannot hold it.
if s.ParentProxy != nil {
node, ok, err := s.ParentProxy.renderTransformedRefWithSiblings(s)
if err != nil {
return nil, err
}
return marshalYAMLNodeJSON(node)
if ok {
return marshalYAMLNodeJSON(node)
}
}

if s.BooleanSchema != nil {
return json.Marshal(*s.BooleanSchema)
}

nb := high.NewNodeBuilder(s, s.low)
Expand All @@ -596,21 +600,22 @@ func (s *Schema) MarshalJSON() ([]byte, error) {
// The ctx parameter should be *InlineRenderContext but is typed as any to satisfy the
// high.RenderableInlineWithContext interface without import cycles.
func (s *Schema) MarshalYAMLInlineWithContext(ctx any) (interface{}, error) {
if node := s.booleanSchemaNode(); node != nil {
return node, nil
}

// ensure we have a valid render context; create default bundle mode context if nil.
// this ensures backward compatibility where nil context = bundle mode behavior.
renderCtx, ok := ctx.(*InlineRenderContext)
if !ok || renderCtx == nil {
renderCtx = NewInlineRenderContext()
ctx = renderCtx
}
// the sibling view of a $ref with siblings renders like its proxy, even when it is boolean.
if s.ParentProxy != nil && s.ParentProxy.isParsedRefWithSiblings() {
return s.ParentProxy.marshalParsedRefWithSiblingsInline(renderCtx, s)
}

if node := s.booleanSchemaNode(); node != nil {
return node, nil
}

// determine if we should preserve discriminator refs based on rendering mode.
// in validation mode, we need to fully inline all refs for the JSON schema compiler.
// in bundle mode (default), we preserve discriminator refs for mapping compatibility.
Expand Down Expand Up @@ -659,11 +664,12 @@ func (s *Schema) MarshalYAMLInline() (interface{}, error) {

// MarshalJSONInline will render out the Schema pointer as JSON, and all refs will be inlined fully
func (s *Schema) MarshalJSONInline() ([]byte, error) {
if s.BooleanSchema != nil {
if s.BooleanSchema != nil && !s.ParentProxy.schemaIsTransformedSiblingView(s) {
return json.Marshal(*s.BooleanSchema)
}

if s.ParentProxy != nil && s.ParentProxy.isParsedRefWithSiblings() {
// the sibling view renders like its proxy, even when it is boolean.
rendered, err := s.MarshalYAMLInline()
if err != nil {
return nil, err
Expand Down
Loading
Loading