From 9f7466ac18fc7dcfb92dca1f2ad5f5c85d93bb17 Mon Sep 17 00:00:00 2001 From: quobix Date: Wed, 1 May 2024 12:59:15 -0400 Subject: [PATCH] tuning example rendering --- renderer/schema_renderer.go | 55 ++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/renderer/schema_renderer.go b/renderer/schema_renderer.go index 8329ab66..b59c7985 100644 --- a/renderer/schema_renderer.go +++ b/renderer/schema_renderer.go @@ -139,16 +139,33 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct maxLength = *schema.MaxLength } + // if there are examples, use them. if schema.Examples != nil && len(schema.Examples) > 0 { var renderedExample any - exmp := schema.Examples[0] - if exmp != nil { - var ex any - _ = exmp.Decode(&ex) - renderedExample = fmt.Sprint(ex) + + // multi examples and the type is an array? then render all examples. + if len(schema.Examples) > 1 && key == itemsType { + 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 + } 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 { @@ -198,30 +215,6 @@ 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) }