Skip to content

Commit

Permalink
fix: fixed extraction of top level named examples to not extract sche…
Browse files Browse the repository at this point in the history
…ma level examples
  • Loading branch information
TristanSpeakEasy authored and daveshanley committed Jul 30, 2024
1 parent f151589 commit 0a98b84
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 2 deletions.
24 changes: 24 additions & 0 deletions datamodel/high/v3/media_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -162,6 +163,29 @@ func TestMediaType_Examples(t *testing.T) {

r := NewMediaType(&n)

assert.Equal(t, 2, orderedmap.Len(r.Examples))

rend, _ := r.Render()
assert.Len(t, rend, 290)
}

func TestMediaType_Examples_NotFromSchema(t *testing.T) {
yml := `schema:
type: string
examples:
- example 1
- example 2
- example 3`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())

var n v3.MediaType
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

r := NewMediaType(&n)

assert.Equal(t, 0, orderedmap.Len(r.Examples))
}
51 changes: 51 additions & 0 deletions datamodel/high/v3/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
package v3

import (
"context"
"strings"
"testing"

"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -169,3 +173,50 @@ func TestParameter_IsDefaultPathEncoding(t *testing.T) {
param := Parameter{}
assert.True(t, param.IsDefaultPathEncoding())
}

func TestParameter_Examples(t *testing.T) {
yml := `examples:
pbjBurger:
summary: A horrible, nutty, sticky mess.
value:
name: Peanut And Jelly
numPatties: 3
cakeBurger:
summary: A sickly, sweet, atrocity
value:
name: Chocolate Cake Burger
numPatties: 5`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())

var n v3.Parameter
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

r := NewParameter(&n)

assert.Equal(t, 2, orderedmap.Len(r.Examples))
}

func TestParameter_Examples_NotFromSchema(t *testing.T) {
yml := `schema:
type: string
examples:
- example 1
- example 2
- example 3`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())

var n v3.Parameter
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

r := NewParameter(&n)

assert.Equal(t, 0, orderedmap.Len(r.Examples))
}
3 changes: 2 additions & 1 deletion datamodel/low/v3/media_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package v3
import (
"context"
"crypto/sha256"
"slices"
"strings"

"github.com/pb33f/libopenapi/datamodel/low"
Expand Down Expand Up @@ -95,7 +96,7 @@ func (mt *MediaType) Build(ctx context.Context, keyNode, root *yaml.Node, idx *i
if eErr != nil {
return eErr
}
if exps != nil {
if exps != nil && slices.Contains(root.Content, expsL) {
mt.Examples = low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*base.Example]]]{
Value: exps,
KeyNode: expsL,
Expand Down
43 changes: 43 additions & 0 deletions datamodel/low/v3/media_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,46 @@ example: a thing`
assert.Equal(t, n.Hash(), n2.Hash())
assert.Equal(t, 1, orderedmap.Len(n.GetExtensions()))
}

func TestMediaType_Examples(t *testing.T) {
yml := `examples:
pbjBurger:
summary: A horrible, nutty, sticky mess.
value:
name: Peanut And Jelly
numPatties: 3
cakeBurger:
summary: A sickly, sweet, atrocity
value:
name: Chocolate Cake Burger
numPatties: 5`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n MediaType
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

assert.Equal(t, 2, orderedmap.Len(n.Examples.Value))
}

func TestMediaType_Examples_NotFromSchema(t *testing.T) {
yml := `schema:
type: string
examples:
- example 1
- example 2
- example 3`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n MediaType
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

assert.Equal(t, 0, orderedmap.Len(n.Examples.Value))
}
4 changes: 3 additions & 1 deletion datamodel/low/v3/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"crypto/sha256"
"fmt"
"slices"
"strings"

"github.com/pb33f/libopenapi/datamodel/low"
Expand Down Expand Up @@ -100,7 +101,8 @@ func (p *Parameter) Build(ctx context.Context, keyNode, root *yaml.Node, idx *in
if eErr != nil {
return eErr
}
if exps != nil {
// Only consider examples if they are defined in the root node.
if exps != nil && slices.Contains(root.Content, expsL) {
p.Examples = low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*base.Example]]]{
Value: exps,
KeyNode: expsL,
Expand Down
43 changes: 43 additions & 0 deletions datamodel/low/v3/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,46 @@ content:
assert.Equal(t, 2, orderedmap.Cast[low.KeyReference[string], low.ValueReference[*base.Example]](n.GetExamples().Value).Len())
assert.Equal(t, 1, orderedmap.Cast[low.KeyReference[string], low.ValueReference[*MediaType]](n.GetContent().Value).Len())
}

func TestParameter_Examples(t *testing.T) {
yml := `examples:
pbjBurger:
summary: A horrible, nutty, sticky mess.
value:
name: Peanut And Jelly
numPatties: 3
cakeBurger:
summary: A sickly, sweet, atrocity
value:
name: Chocolate Cake Burger
numPatties: 5`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n Parameter
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

assert.Equal(t, 2, orderedmap.Len(n.Examples.Value))
}

func TestParameter_Examples_NotFromSchema(t *testing.T) {
yml := `schema:
type: string
examples:
- example 1
- example 2
- example 3`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n Parameter
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

assert.Equal(t, 0, orderedmap.Len(n.Examples.Value))
}

0 comments on commit 0a98b84

Please sign in to comment.