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

Generate synthetic fixtures for nullable types when expansions are requested #446

Merged
merged 3 commits into from
Aug 25, 2023
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
20 changes: 13 additions & 7 deletions server/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ func (g *DataGenerator) generateInternal(params *GenerateParams) (interface{}, e
}

// Generate a synthethic schema as a last ditch effort
if example == nil && schema.XResourceID == "" {
example = &valueWrapper{value: generateSyntheticFixture(schema, context)}
if (example == nil || example.value == nil) && schema.XResourceID == "" {
example = &valueWrapper{value: generateSyntheticFixture(schema, context, params.Expansions)}

context = fmt.Sprintf("%sGenerated synthetic fixture: %+v\n", context, schema)

Expand Down Expand Up @@ -734,12 +734,12 @@ func distributeReplacedIDsInValue(pathParams *PathParamsMap, value interface{})
// This function calls itself recursively by initially iterating through every
// property in an object schema, then recursing and returning values for
// embedded objects and scalars.
func generateSyntheticFixture(schema *spec.Schema, context string) interface{} {
func generateSyntheticFixture(schema *spec.Schema, context string, expansions *ExpansionLevel) interface{} {
context = fmt.Sprintf("%sGenerating synthetic fixture: %+v\n", context, schema)

// Return the minimum viable object by returning nil/null for a nullable
// property.
if schema.Nullable {
// property, if that property does not need to be expanded.
if schema.Nullable && expansions == nil {
return nil
}

Expand All @@ -755,7 +755,8 @@ func generateSyntheticFixture(schema *spec.Schema, context string) interface{} {
if subSchema.Ref != "" {
continue
}
return generateSyntheticFixture(subSchema, context)

return generateSyntheticFixture(subSchema, context, expansions)
}
panic(fmt.Sprintf("%sCouldn't find an anyOf branch to take", context))
}
Expand All @@ -782,7 +783,12 @@ func generateSyntheticFixture(schema *spec.Schema, context string) interface{} {
continue
}

fixture[property] = generateSyntheticFixture(subSchema, context)
var propertyExpansions *ExpansionLevel
if expansions != nil && expansions.expansions[property] != nil {
propertyExpansions = expansions.expansions[property]
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

fixture[property] = generateSyntheticFixture(subSchema, context, propertyExpansions)
}
return fixture

Expand Down
64 changes: 55 additions & 9 deletions server/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,31 +812,31 @@ func TestFindAnyOfBranch(t *testing.T) {

func TestGenerateSyntheticFixture(t *testing.T) {
// Scalars (and an array, which is easy)
assert.Equal(t, []string{}, generateSyntheticFixture(&spec.Schema{Type: spec.TypeArray}, ""))
assert.Equal(t, true, generateSyntheticFixture(&spec.Schema{Type: spec.TypeBoolean}, ""))
assert.Equal(t, 0, generateSyntheticFixture(&spec.Schema{Type: spec.TypeInteger}, ""))
assert.Equal(t, 0.0, generateSyntheticFixture(&spec.Schema{Type: spec.TypeNumber}, ""))
assert.Equal(t, "", generateSyntheticFixture(&spec.Schema{Type: spec.TypeString}, ""))
assert.Equal(t, []string{}, generateSyntheticFixture(&spec.Schema{Type: spec.TypeArray}, "", nil))
assert.Equal(t, true, generateSyntheticFixture(&spec.Schema{Type: spec.TypeBoolean}, "", nil))
assert.Equal(t, 0, generateSyntheticFixture(&spec.Schema{Type: spec.TypeInteger}, "", nil))
assert.Equal(t, 0.0, generateSyntheticFixture(&spec.Schema{Type: spec.TypeNumber}, "", nil))
assert.Equal(t, "", generateSyntheticFixture(&spec.Schema{Type: spec.TypeString}, "", nil))

// Nullable property
assert.Equal(t, nil, generateSyntheticFixture(&spec.Schema{
Nullable: true,
Type: spec.TypeString,
}, ""))
}, "", nil))

// Property with enum
assert.Equal(t, "list", generateSyntheticFixture(&spec.Schema{
Enum: []interface{}{"list"},
Type: spec.TypeString,
}, ""))
}, "", nil))

// Takes the first non-reference branch of an anyOf
assert.Equal(t, "", generateSyntheticFixture(&spec.Schema{
AnyOf: []*spec.Schema{
{Ref: "#/components/schemas/radar_rule"},
{Type: spec.TypeString},
},
}, ""))
}, "", nil))

// Object
assert.Equal(t,
Expand Down Expand Up @@ -866,8 +866,54 @@ func TestGenerateSyntheticFixture(t *testing.T) {
"object",
"url",
},
}, ""),
}, "", nil),
)

// Nullable object property with expansion
assert.Equal(t,
map[string]interface{}{
"foo": "",
},
generateSyntheticFixture(&spec.Schema{
Type: "object",
Nullable: true,
Properties: map[string]*spec.Schema{
"foo": {
Type: "string",
},
},
Required: []string{
"foo",
},
}, "", &ExpansionLevel{
expansions: map[string]*ExpansionLevel{"foo": {
expansions: map[string]*ExpansionLevel{}},
},
}),
)
}

func TestGenerateForNullableExpansion(t *testing.T) {
generator := DataGenerator{
definitions: realSpec.Components.Schemas,
fixtures: &realFixtures,
verbose: verbose,
}

var example interface{}
var err error
assert.NotPanics(t, func() {
example, err = generator.Generate(&GenerateParams{
Expansions: &ExpansionLevel{
expansions: map[string]*ExpansionLevel{"account_tax_ids": {
expansions: map[string]*ExpansionLevel{}},
},
},
Schema: &spec.Schema{Ref: "#/components/schemas/invoice"},
})
})
assert.NoError(t, err)
assert.NotNil(t, example)
}

func TestPropertyNames(t *testing.T) {
Expand Down