mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-11 04:20:24 +00:00
Added item example rendering to schema renderer
Addresses https://github.com/pb33f/wiretap/issues/90
This commit is contained in:
@@ -184,6 +184,31 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
|
||||
structure[key] = str
|
||||
}
|
||||
} else {
|
||||
if key == itemsType {
|
||||
// check if the schema contains an example
|
||||
if schema.Example != nil {
|
||||
var example any
|
||||
_ = schema.Example.Decode(&example)
|
||||
structure[key] = fmt.Sprint(example)
|
||||
return
|
||||
}
|
||||
// if the schema contains examples, then render them.
|
||||
if schema.Examples != nil {
|
||||
if len(schema.Examples) > 0 {
|
||||
renderedExamples := make([]any, len(schema.Examples))
|
||||
for i, exmp := range schema.Examples {
|
||||
if exmp != nil {
|
||||
var ex any
|
||||
_ = exmp.Decode(&ex)
|
||||
renderedExamples[i] = fmt.Sprint(ex)
|
||||
}
|
||||
}
|
||||
structure[key] = renderedExamples
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
// last resort, generate a random value
|
||||
structure[key] = wr.RandomWord(minLength, maxLength, 0)
|
||||
}
|
||||
}
|
||||
@@ -331,7 +356,11 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
|
||||
itemMap := make(map[string]any)
|
||||
itemsSchemaCompiled := itemsSchema.A.Schema()
|
||||
wr.DiveIntoSchema(itemsSchemaCompiled, itemsType, itemMap, depth+1)
|
||||
renderedItems = append(renderedItems, itemMap[itemsType])
|
||||
if multipleItems, ok := itemMap[itemsType].([]any); ok {
|
||||
renderedItems = multipleItems
|
||||
} else {
|
||||
renderedItems = append(renderedItems, itemMap[itemsType])
|
||||
}
|
||||
}
|
||||
structure[key] = renderedItems
|
||||
return
|
||||
|
||||
@@ -1034,6 +1034,54 @@ properties:
|
||||
assert.Equal(t, `{"count":9934}`, string(rendered))
|
||||
}
|
||||
|
||||
func TestRenderSchema_Items_WithExample(t *testing.T) {
|
||||
testObject := `type: object
|
||||
properties:
|
||||
args:
|
||||
type: object
|
||||
properties:
|
||||
arrParam:
|
||||
type: string
|
||||
example: test,test2
|
||||
arrParamExploded:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
example: "1"`
|
||||
|
||||
compiled := getSchema([]byte(testObject))
|
||||
schema := make(map[string]any)
|
||||
wr := createSchemaRenderer()
|
||||
wr.DiveIntoSchema(compiled, "pb33f", schema, 0)
|
||||
rendered, _ := json.Marshal(schema["pb33f"])
|
||||
assert.Equal(t, `{"args":{"arrParam":"test,test2","arrParamExploded":["1"]}}`, string(rendered))
|
||||
}
|
||||
|
||||
func TestRenderSchema_Items_WithExamples(t *testing.T) {
|
||||
testObject := `type: object
|
||||
properties:
|
||||
args:
|
||||
type: object
|
||||
properties:
|
||||
arrParam:
|
||||
type: string
|
||||
example: test,test2
|
||||
arrParamExploded:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
examples:
|
||||
- 1
|
||||
- 2`
|
||||
|
||||
compiled := getSchema([]byte(testObject))
|
||||
schema := make(map[string]any)
|
||||
wr := createSchemaRenderer()
|
||||
wr.DiveIntoSchema(compiled, "pb33f", schema, 0)
|
||||
rendered, _ := json.Marshal(schema["pb33f"])
|
||||
assert.Equal(t, `{"args":{"arrParam":"test,test2","arrParamExploded":["1","2"]}}`, string(rendered))
|
||||
}
|
||||
|
||||
func TestCreateRendererUsingDefaultDictionary(t *testing.T) {
|
||||
assert.NotNil(t, CreateRendererUsingDefaultDictionary())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user