mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
tuning example rendering
This commit is contained in:
@@ -139,16 +139,33 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
|
|||||||
maxLength = *schema.MaxLength
|
maxLength = *schema.MaxLength
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if there are examples, use them.
|
||||||
if schema.Examples != nil && len(schema.Examples) > 0 {
|
if schema.Examples != nil && len(schema.Examples) > 0 {
|
||||||
var renderedExample any
|
var renderedExample any
|
||||||
exmp := schema.Examples[0]
|
|
||||||
if exmp != nil {
|
// multi examples and the type is an array? then render all examples.
|
||||||
var ex any
|
if len(schema.Examples) > 1 && key == itemsType {
|
||||||
_ = exmp.Decode(&ex)
|
renderedExamples := make([]any, len(schema.Examples))
|
||||||
renderedExample = fmt.Sprint(ex)
|
for i, exmp := range schema.Examples {
|
||||||
|
if exmp != nil {
|
||||||
|
var ex any
|
||||||
|
_ = exmp.Decode(&ex)
|
||||||
|
renderedExamples[i] = fmt.Sprint(ex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
structure[key] = renderedExamples
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// render the first example
|
||||||
|
exmp := schema.Examples[0]
|
||||||
|
if exmp != nil {
|
||||||
|
var ex any
|
||||||
|
_ = exmp.Decode(&ex)
|
||||||
|
renderedExample = fmt.Sprint(ex)
|
||||||
|
}
|
||||||
|
structure[key] = renderedExample
|
||||||
|
return
|
||||||
}
|
}
|
||||||
structure[key] = renderedExample
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch schema.Format {
|
switch schema.Format {
|
||||||
@@ -198,30 +215,6 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
|
|||||||
structure[key] = str
|
structure[key] = str
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
// last resort, generate a random value
|
||||||
structure[key] = wr.RandomWord(minLength, maxLength, 0)
|
structure[key] = wr.RandomWord(minLength, maxLength, 0)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user