Test rendering of examples for all properties in the schema

This commit is contained in:
Martins Irbe
2024-05-22 14:04:11 +01:00
committed by quobix
parent 711da3bd46
commit c1bba8ae5a

View File

@@ -12,6 +12,7 @@ import (
"github.com/pb33f/libopenapi/orderedmap" "github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils" "github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings" "strings"
"testing" "testing"
@@ -410,3 +411,41 @@ properties:
assert.Equal(t, "robocop", m["name"].(string)) assert.Equal(t, "robocop", m["name"].(string))
assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string)) assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string))
} }
// Test schema rendering for property examples, regardless of whether the property
// is marked as required or not.
func TestMockGenerator_GenerateExamplesForAllProperties(t *testing.T) {
yml := `type: object
required:
- id
- name
properties:
id:
type: integer
example: 123
name:
type: string
example: "John Doe"
active:
type: boolean
example: true
balance:
type: number
format: float
example: 99.99
tags:
type: array
items:
type: string
example: ["tag1", "tag2", "tag3"]
`
fake := createFakeMock(yml, nil, nil)
mg := NewMockGenerator(JSON)
mock, err := mg.GenerateMock(fake, "")
require.NoError(t, err)
assert.Equal(t, `{"active":true,"balance":99.99,"id":123,"name":"John Doe","tags":["tag1","tag2","tag3"]}`, string(mock))
}