Skip to content

Commit

Permalink
fix: fixed MediaType example being populated from wrong source
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy authored and daveshanley committed Apr 22, 2024
1 parent 18a817b commit 6f277db
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datamodel/low/v3/media_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (mt *MediaType) Build(ctx context.Context, keyNode, root *yaml.Node, idx *i
mt.Extensions = low.ExtractExtensions(root)

// handle example if set.
_, expLabel, expNode := utils.FindKeyNodeFull(base.ExampleLabel, root.Content)
_, expLabel, expNode := utils.FindKeyNodeFullTop(base.ExampleLabel, root.Content)
if expNode != nil {
mt.Example = low.NodeReference[*yaml.Node]{Value: expNode, KeyNode: expLabel, ValueNode: expNode}
}
Expand Down
45 changes: 45 additions & 0 deletions datamodel/low/v3/request_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,48 @@ x-toast: nice`
// hash
assert.Equal(t, n.Hash(), n2.Hash())
}

func TestRequestBody_TopLevelExampleExtraction(t *testing.T) {
getExample := func(yml string) string {
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n RequestBody
err := low.BuildModel(idxNode.Content[0], &n)
assert.NoError(t, err)

err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.NoError(t, err)

var example string

content := n.FindContent("fresh/fish")
if content == nil || content.Value == nil {
return ""
}

if content.Value.Example.Value == nil {
return ""
}

err = content.Value.Example.Value.Decode(&example)
assert.NoError(t, err)

return example
}

topLevelYml := `content:
fresh/fish:
example: nice.`
topLevelExample := getExample(topLevelYml)
assert.Equal(t, "nice.", topLevelExample)

schemaLevelYml := `content:
fresh/fish:
schema:
type: string
example: nice.`
schemaLevelExample := getExample(schemaLevelYml)
assert.Equal(t, "", schemaLevelExample)
}

0 comments on commit 6f277db

Please sign in to comment.