Skip to content

Commit

Permalink
Don't generate example shape values if they don't exist in the model. (
Browse files Browse the repository at this point in the history
…#4108)

* Don't generate shape values if they don't exist in the model.
* Skip non-existent operations
  • Loading branch information
skmcgrail authored Sep 22, 2021
1 parent f6af9ec commit 202e7eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
16 changes: 11 additions & 5 deletions private/model/api/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,16 @@ func (p *ExamplesDefinition) setup() error {
builder = NewExamplesBuilder()
}

keys := p.Examples.Names()
for _, n := range keys {
filteredExamples := make(Examples)

for _, n := range p.Examples.Names() {
examples := p.Examples[n]
n = p.ExportableName(n)
if _, ok := p.API.Operations[n]; !ok {
continue
}
filteredExamples[n] = make([]Example, 0, len(examples))
for i, e := range examples {
n = p.ExportableName(n)
e.OperationName = n
e.API = p.API
e.Index = fmt.Sprintf("shared%02d", i)
Expand All @@ -235,12 +240,13 @@ func (p *ExamplesDefinition) setup() error {

e.VisitedErrors = map[string]struct{}{}
op := p.API.Operations[e.OperationName]
e.OperationName = p.ExportableName(e.OperationName)
e.Operation = op
p.Examples[n][i] = e

filteredExamples[n] = append(filteredExamples[n], e)
}
}

p.Examples = filteredExamples
p.API.Examples = p.Examples

return nil
Expand Down
58 changes: 36 additions & 22 deletions private/model/api/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,34 +170,34 @@ func buildAPI() *API {
}

func TestExampleGeneration(t *testing.T) {
example := `
{
example := `{
"version": "1.0",
"examples": {
"Foo": [
{
"input": {
"BarShape": "Hello world",
"ComplexField": {
"Field": "bar",
"List": [
{
"NestedField": "qux"
}
]
},
"ListField": [
{
"Field": "baz"
}
],
"ListsField": [
[
{
"Field": "baz"
}
]
]
"ComplexField": {
"Field": "bar",
"List": [
{
"NestedField": "qux"
}
]
},
"ListField": [
{
"Field": "baz"
}
],
"ListsField": [
[
{
"Field": "baz"
}
]
],
"FieldDoesNotExistInModel": true
},
"output": {
"BazShape": 1
Expand All @@ -211,6 +211,20 @@ func TestExampleGeneration(t *testing.T) {
"description": "Foo bar baz qux",
"title": "I pity the foo"
}
],
"NotInTheModel": [
{
"input": {},
"output": {},
"comments": {
"input": {
},
"output": {
}
},
"description": "Look the other way",
"title": "I am not modeled"
}
]
}
}
Expand Down
4 changes: 4 additions & 0 deletions private/model/api/shape_value_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ func (b ShapeValueBuilder) BuildComplex(name, memName string, ref *ShapeRef, par
},
`, memName, b.GoType(ref, true), b.BuildShape(ref, v, true))
} else {
// Don't try to generate for members not actually modeled in the API
if _, ok := parent.MemberRefs[memName]; !ok {
return ""
}
return fmt.Sprintf(`%s: &%s{
%s
},
Expand Down

0 comments on commit 202e7eb

Please sign in to comment.