mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
Adding more schema rendering tests
This commit is contained in:
@@ -139,6 +139,18 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
|
|||||||
maxLength = *schema.MaxLength
|
maxLength = *schema.MaxLength
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
structure[key] = renderedExample
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch schema.Format {
|
switch schema.Format {
|
||||||
case dateTimeType:
|
case dateTimeType:
|
||||||
structure[key] = time.Now().Format(time.RFC3339)
|
structure[key] = time.Now().Format(time.RFC3339)
|
||||||
@@ -243,23 +255,16 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
|
|||||||
maximum = int64(*schema.Maximum)
|
maximum = int64(*schema.Maximum)
|
||||||
}
|
}
|
||||||
|
|
||||||
if schema.Example != nil {
|
|
||||||
var example any
|
|
||||||
_ = schema.Example.Decode(&example)
|
|
||||||
structure[key] = example
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if schema.Examples != nil {
|
if schema.Examples != nil {
|
||||||
if len(schema.Examples) > 0 {
|
if len(schema.Examples) > 0 {
|
||||||
renderedExamples := make([]any, len(schema.Examples))
|
var renderedExample any
|
||||||
for i, exmp := range schema.Examples {
|
exmp := schema.Examples[0]
|
||||||
if exmp != nil {
|
if exmp != nil {
|
||||||
var ex any
|
var ex any
|
||||||
_ = exmp.Decode(&ex)
|
_ = exmp.Decode(&ex)
|
||||||
renderedExamples[i] = fmt.Sprint(ex)
|
renderedExample = ex
|
||||||
}
|
}
|
||||||
}
|
structure[key] = renderedExample
|
||||||
structure[key] = renderedExamples
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1111,6 +1111,38 @@ properties:
|
|||||||
assert.Equal(t, `{"bigint":8821239038968084,"bigintStr":"9223372036854775808","decimal":3.141592653589793,"decimalStr":"3.14159265358979344719667586"}`, string(rendered))
|
assert.Equal(t, `{"bigint":8821239038968084,"bigintStr":"9223372036854775808","decimal":3.141592653589793,"decimalStr":"3.14159265358979344719667586"}`, string(rendered))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRenderSchema_NonStandard_Format_MultiExample(t *testing.T) {
|
||||||
|
testObject := `type: object
|
||||||
|
properties:
|
||||||
|
bigint:
|
||||||
|
type: integer
|
||||||
|
format: bigint
|
||||||
|
examples:
|
||||||
|
- 8821239038968084
|
||||||
|
bigintStr:
|
||||||
|
type: string
|
||||||
|
format: bigint
|
||||||
|
examples:
|
||||||
|
- "9223372036854775808"
|
||||||
|
decimal:
|
||||||
|
type: number
|
||||||
|
format: decimal
|
||||||
|
examples:
|
||||||
|
- 3.141592653589793
|
||||||
|
decimalStr:
|
||||||
|
type: string
|
||||||
|
format: decimal
|
||||||
|
examples:
|
||||||
|
- "3.14159265358979344719667586"`
|
||||||
|
|
||||||
|
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, `{"bigint":8821239038968084,"bigintStr":"9223372036854775808","decimal":3.141592653589793,"decimalStr":"3.14159265358979344719667586"}`, string(rendered))
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateRendererUsingDefaultDictionary(t *testing.T) {
|
func TestCreateRendererUsingDefaultDictionary(t *testing.T) {
|
||||||
assert.NotNil(t, CreateRendererUsingDefaultDictionary())
|
assert.NotNil(t, CreateRendererUsingDefaultDictionary())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user