mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
Added correct handling of schema example rendering
reported in https://github.com/pb33f/wiretap/issues/84 Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
@@ -453,7 +453,7 @@ func TestSpecIndex_BaseURLError(t *testing.T) {
|
|||||||
files := remoteFS.GetFiles()
|
files := remoteFS.GetFiles()
|
||||||
fileLen := len(files)
|
fileLen := len(files)
|
||||||
assert.Equal(t, 0, fileLen)
|
assert.Equal(t, 0, fileLen)
|
||||||
assert.GreaterOrEqual(t, len(remoteFS.GetErrors()), 200)
|
assert.GreaterOrEqual(t, len(remoteFS.GetErrors()), 155)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSpecIndex_k8s(t *testing.T) {
|
func TestSpecIndex_k8s(t *testing.T) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
|
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
|
||||||
"github.com/pb33f/libopenapi/orderedmap"
|
"github.com/pb33f/libopenapi/orderedmap"
|
||||||
@@ -154,6 +155,27 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if schemaValue != nil {
|
if schemaValue != nil {
|
||||||
|
|
||||||
|
// now lets check the schema for `Examples` and `Example` fields.
|
||||||
|
if schemaValue.Examples != nil {
|
||||||
|
if name != "" {
|
||||||
|
// try and convert the example to an integer
|
||||||
|
if i, err := strconv.Atoi(name); err == nil {
|
||||||
|
if i < len(schemaValue.Examples) {
|
||||||
|
return mg.renderMock(schemaValue.Examples[i]), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if the name is empty, just return the first example
|
||||||
|
return mg.renderMock(schemaValue.Examples[0]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the example field
|
||||||
|
if schemaValue.Example != nil {
|
||||||
|
return mg.renderMock(schemaValue.Example), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// render the schema as our last hope.
|
||||||
renderMap := mg.renderer.RenderSchema(schemaValue)
|
renderMap := mg.renderer.RenderSchema(schemaValue)
|
||||||
if renderMap != nil {
|
if renderMap != nil {
|
||||||
return mg.renderMock(renderMap), nil
|
return mg.renderMock(renderMap), nil
|
||||||
|
|||||||
@@ -323,3 +323,94 @@ func TestMockGenerator_GenerateMock_YamlNode_Nil(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "hello", strings.TrimSpace(string(mock)))
|
assert.Equal(t, "hello", strings.TrimSpace(string(mock)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMockGenerator_GenerateJSONMock_Object_SchemaExamples(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `type: object
|
||||||
|
examples:
|
||||||
|
- name: happy days
|
||||||
|
description: a terrible show from a time that never existed.
|
||||||
|
- name: robocop
|
||||||
|
description: perhaps the best cyberpunk movie ever made.
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: nameExample
|
||||||
|
description:
|
||||||
|
type: string
|
||||||
|
example: descriptionExample`
|
||||||
|
|
||||||
|
fake := createFakeMock(yml, nil, nil)
|
||||||
|
mg := NewMockGenerator(YAML)
|
||||||
|
mock, err := mg.GenerateMock(fake, "")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// re-serialize back into a map and check the values
|
||||||
|
var m map[string]any
|
||||||
|
err = yaml.Unmarshal(mock, &m)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Len(t, m, 2)
|
||||||
|
assert.Equal(t, "happy days", m["name"].(string))
|
||||||
|
assert.Equal(t, "a terrible show from a time that never existed.", m["description"].(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMockGenerator_GenerateJSONMock_Object_SchemaExamples_Preferred(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `type: object
|
||||||
|
examples:
|
||||||
|
- name: happy days
|
||||||
|
description: a terrible show from a time that never existed.
|
||||||
|
- name: robocop
|
||||||
|
description: perhaps the best cyberpunk movie ever made.
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: nameExample
|
||||||
|
description:
|
||||||
|
type: string
|
||||||
|
example: descriptionExample`
|
||||||
|
|
||||||
|
fake := createFakeMock(yml, nil, nil)
|
||||||
|
mg := NewMockGenerator(YAML)
|
||||||
|
mock, err := mg.GenerateMock(fake, "1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// re-serialize back into a map and check the values
|
||||||
|
var m map[string]any
|
||||||
|
err = yaml.Unmarshal(mock, &m)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Len(t, m, 2)
|
||||||
|
assert.Equal(t, "robocop", m["name"].(string))
|
||||||
|
assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMockGenerator_GenerateJSONMock_Object_SchemaExample(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `type: object
|
||||||
|
example:
|
||||||
|
name: robocop
|
||||||
|
description: perhaps the best cyberpunk movie ever made.
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: nameExample
|
||||||
|
description:
|
||||||
|
type: string
|
||||||
|
example: descriptionExample`
|
||||||
|
|
||||||
|
fake := createFakeMock(yml, nil, nil)
|
||||||
|
mg := NewMockGenerator(YAML)
|
||||||
|
mock, err := mg.GenerateMock(fake, "")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// re-serialize back into a map and check the values
|
||||||
|
var m map[string]any
|
||||||
|
err = yaml.Unmarshal(mock, &m)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Len(t, m, 2)
|
||||||
|
assert.Equal(t, "robocop", m["name"].(string))
|
||||||
|
assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user