diff --git a/private/model/api/example.go b/private/model/api/example.go index 94083aabfbc..ed1eeb4b3f5 100644 --- a/private/model/api/example.go +++ b/private/model/api/example.go @@ -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) @@ -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 diff --git a/private/model/api/example_test.go b/private/model/api/example_test.go index 1497fc9d297..0403323cc8c 100644 --- a/private/model/api/example_test.go +++ b/private/model/api/example_test.go @@ -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 @@ -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" + } ] } } diff --git a/private/model/api/shape_value_builder.go b/private/model/api/shape_value_builder.go index 80cf8fd9dbd..08fb5e5cc71 100644 --- a/private/model/api/shape_value_builder.go +++ b/private/model/api/shape_value_builder.go @@ -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 },