Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mock generator to allow disabling required check in schema renderer #295

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions renderer/mock_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (mg *MockGenerator) SetPretty() {
mg.pretty = true
}

// DisableRequiredCheck disables renderer required property check when rendering
// a schema for mocks. This means that all properties will be rendered, not just
// the required ones.
func (mg *MockGenerator) DisableRequiredCheck() {
mg.renderer.DisableRequiredCheck()
}

// GenerateMock generates a mock for a given high-level mockable struct. The mockable struct must contain the following fields:
// Example: any type, this is the default example to use if no examples are present.
// Examples: *orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
Expand Down
63 changes: 63 additions & 0 deletions renderer/mock_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"strings"
"testing"
Expand Down Expand Up @@ -410,3 +411,65 @@ properties:
assert.Equal(t, "robocop", m["name"].(string))
assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string))
}

func TestMockGenerator_GeneratePropertyExamples(t *testing.T) {
fake := createFakeMock(`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"]
`, nil, nil)

for name, tc := range map[string]struct {
mockGen func() *MockGenerator
expectedMock string
}{
"OnlyRequired": {
mockGen: func() *MockGenerator {
mg := NewMockGenerator(JSON)
return mg
},
expectedMock: `{"id":123,"name":"John Doe"}`,
},
"All": {
mockGen: func() *MockGenerator {
mg := NewMockGenerator(JSON)

// Test schema rendering for property examples, regardless of
// whether the property is marked as required or not.
mg.DisableRequiredCheck()

return mg
},
expectedMock: `{"active":true,"balance":99.99,"id":123,"name":"John Doe","tags":["tag1","tag2","tag3"]}`,
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()

mock, err := tc.mockGen().GenerateMock(fake, "")
require.NoError(t, err)

assert.Equal(t, tc.expectedMock, string(mock))
})
}
}
Loading