Skip to content

Commit 3bb5b77

Browse files
committed
Copy schema example code to its own file
1 parent 582b161 commit 3bb5b77

File tree

4 files changed

+281
-273
lines changed

4 files changed

+281
-273
lines changed

apisprout.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -159,62 +159,6 @@ func getTypedExample(mt *openapi3.MediaType) (interface{}, error) {
159159
return nil, ErrNoExample
160160
}
161161

162-
// getTypedExampleFromSchema will return an example from a given schema
163-
func getTypedExampleFromSchema(schema *openapi3.Schema) (interface{}, error) {
164-
if schema.Example != nil {
165-
return schema.Example, nil
166-
}
167-
168-
if schema.Type == "number" {
169-
return 0, nil
170-
}
171-
if schema.Type == "integer" {
172-
return 0, nil
173-
}
174-
if schema.Type == "boolean" {
175-
return true, nil
176-
}
177-
if schema.Type == "string" {
178-
return "string", nil
179-
}
180-
if schema.Type == "array" {
181-
example := []interface{}{}
182-
if schema.Items != nil && schema.Items.Value != nil {
183-
ex, err := getTypedExampleFromSchema(schema.Items.Value)
184-
if err != nil {
185-
return nil, fmt.Errorf("can't get example for array item")
186-
}
187-
example = append(example, ex)
188-
}
189-
return example, nil
190-
}
191-
192-
if schema.Type == "object" || len(schema.Properties) > 0 {
193-
example := map[string]interface{}{}
194-
for k, v := range schema.Properties {
195-
ex, err := getTypedExampleFromSchema(v.Value)
196-
if err != nil {
197-
return nil, fmt.Errorf("can't get example for '%s'", k)
198-
}
199-
example[k] = ex
200-
}
201-
202-
if schema.AdditionalProperties != nil && schema.AdditionalProperties.Value != nil {
203-
addl := schema.AdditionalProperties.Value
204-
ex, err := getTypedExampleFromSchema(addl)
205-
if err != nil {
206-
return nil, fmt.Errorf("can't get example for additional properties")
207-
}
208-
209-
example["additionalPropertyName"] = ex
210-
}
211-
212-
return example, nil
213-
}
214-
215-
return nil, ErrNoExample
216-
}
217-
218162
// getExample tries to return an example for a given operation.
219163
func getExample(negotiator *ContentNegotiator, prefer string, op *openapi3.Operation) (int, string, interface{}, error) {
220164
var responses []string

apisprout_test.go

Lines changed: 0 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1,218 +1 @@
11
package main
2-
3-
import (
4-
"encoding/json"
5-
"testing"
6-
7-
"github.com/getkin/kin-openapi/openapi3"
8-
"github.com/stretchr/testify/assert"
9-
)
10-
11-
type getTypedExampleTestData struct {
12-
name string
13-
generateInputSchema func() *openapi3.Schema
14-
validateResult func(*testing.T, string)
15-
}
16-
17-
func Test_GetTypedExampleShouldGetFromExampleField(t *testing.T) {
18-
exampleData := map[string]string{"name1": "value1", "name2": "value2"}
19-
20-
mediaType := openapi3.NewMediaType()
21-
mediaType.WithExample("example1", exampleData)
22-
23-
selectedExample, err := getTypedExample(mediaType)
24-
assert.Nil(t, err)
25-
26-
selectedExampleJSON, err := json.Marshal(selectedExample)
27-
assert.Nil(t, err)
28-
assert.NotEmpty(t, string(selectedExampleJSON))
29-
assert.Equal(t, `{"name1":"value1","name2":"value2"}`, string(selectedExampleJSON))
30-
}
31-
32-
var getTypedExampleTestDataEntries = []getTypedExampleTestData{
33-
getTypedExampleTestData{
34-
name: "SchemaExampleField",
35-
generateInputSchema: func() *openapi3.Schema {
36-
exampleData := map[string]string{"name1": "value1", "name2": "value2"}
37-
schema := openapi3.NewSchema()
38-
schema.Example = exampleData
39-
return schema
40-
},
41-
validateResult: func(t *testing.T, s string) {
42-
assert.Equal(t, `{"name1":"value1","name2":"value2"}`, s)
43-
},
44-
},
45-
getTypedExampleTestData{
46-
name: "SchemaPropertiesExampleField",
47-
generateInputSchema: func() *openapi3.Schema {
48-
schema := openapi3.NewSchema()
49-
50-
parameterSchema1 := openapi3.NewStringSchema()
51-
parameterSchema1.Example = "testvalue"
52-
53-
parameterSchema2 := openapi3.NewObjectSchema()
54-
nestedParameterSchema := openapi3.NewBoolSchema()
55-
nestedParameterSchema.Example = true
56-
parameterSchema2.WithProperty("nestedProperty", nestedParameterSchema)
57-
58-
schema.WithProperties(map[string]*openapi3.Schema{
59-
"name1": parameterSchema1,
60-
"name2": parameterSchema2,
61-
})
62-
return schema
63-
},
64-
validateResult: func(t *testing.T, s string) {
65-
assert.Equal(t, `{"name1":"testvalue","name2":{"nestedProperty":true}}`, s)
66-
},
67-
},
68-
getTypedExampleTestData{
69-
name: "SchemaArrayItemsStringExampleField",
70-
generateInputSchema: func() *openapi3.Schema {
71-
schema := openapi3.NewArraySchema()
72-
itemSchema := openapi3.NewStringSchema()
73-
itemSchema.Example = "testvalue"
74-
schema.WithItems(itemSchema)
75-
return schema
76-
},
77-
validateResult: func(t *testing.T, s string) {
78-
assert.Equal(t, `["testvalue"]`, s)
79-
},
80-
},
81-
getTypedExampleTestData{
82-
name: "SchemaArrayItemsObjectExampleField",
83-
generateInputSchema: func() *openapi3.Schema {
84-
schema := openapi3.NewArraySchema()
85-
86-
itemSchema := openapi3.NewObjectSchema()
87-
88-
parameterSchema1 := openapi3.NewStringSchema()
89-
parameterSchema1.Example = "testvalue"
90-
91-
itemSchema.WithProperties(map[string]*openapi3.Schema{
92-
"name1": parameterSchema1,
93-
})
94-
95-
schema.WithItems(itemSchema)
96-
return schema
97-
},
98-
validateResult: func(t *testing.T, s string) {
99-
assert.Equal(t, `[{"name1":"testvalue"}]`, s)
100-
},
101-
},
102-
getTypedExampleTestData{
103-
name: "StringSchemaWithExample",
104-
generateInputSchema: func() *openapi3.Schema {
105-
schema := openapi3.NewStringSchema()
106-
schema.Example = "examplestr"
107-
return schema
108-
},
109-
validateResult: func(t *testing.T, s string) {
110-
assert.Equal(t, `"examplestr"`, s)
111-
},
112-
},
113-
getTypedExampleTestData{
114-
name: "StringSchemaWithoutExample",
115-
generateInputSchema: func() *openapi3.Schema {
116-
schema := openapi3.NewStringSchema()
117-
return schema
118-
},
119-
validateResult: func(t *testing.T, s string) {
120-
assert.Equal(t, `"string"`, s)
121-
},
122-
},
123-
getTypedExampleTestData{
124-
name: "BooleanSchema",
125-
generateInputSchema: func() *openapi3.Schema {
126-
schema := openapi3.NewBoolSchema()
127-
return schema
128-
},
129-
validateResult: func(t *testing.T, s string) {
130-
assert.Equal(t, `true`, s)
131-
},
132-
},
133-
getTypedExampleTestData{
134-
name: "IntegerSchemaWithoutExample",
135-
generateInputSchema: func() *openapi3.Schema {
136-
schema := openapi3.NewIntegerSchema()
137-
return schema
138-
},
139-
validateResult: func(t *testing.T, s string) {
140-
assert.Equal(t, `0`, s)
141-
},
142-
},
143-
getTypedExampleTestData{
144-
name: "IntegerSchemaWithExample",
145-
generateInputSchema: func() *openapi3.Schema {
146-
schema := openapi3.NewIntegerSchema()
147-
schema.Example = 1
148-
return schema
149-
},
150-
validateResult: func(t *testing.T, s string) {
151-
assert.Equal(t, `1`, s)
152-
},
153-
},
154-
getTypedExampleTestData{
155-
name: "NumberSchemaWithoutExample",
156-
generateInputSchema: func() *openapi3.Schema {
157-
schema := openapi3.NewSchema()
158-
schema.Type = "number"
159-
return schema
160-
},
161-
validateResult: func(t *testing.T, s string) {
162-
assert.Equal(t, `0`, s)
163-
},
164-
},
165-
getTypedExampleTestData{
166-
name: "NumberSchemaWithExample",
167-
generateInputSchema: func() *openapi3.Schema {
168-
schema := openapi3.NewSchema()
169-
schema.Type = "number"
170-
schema.Example = 1.1
171-
return schema
172-
},
173-
validateResult: func(t *testing.T, s string) {
174-
assert.Equal(t, `1.1`, s)
175-
},
176-
},
177-
}
178-
179-
func Test_GetTypedExampleTest(t *testing.T) {
180-
181-
for _, td := range getTypedExampleTestDataEntries {
182-
t.Logf("testcase: '%s'", td.name)
183-
184-
mediaType := openapi3.NewMediaType()
185-
mediaType.WithSchema(td.generateInputSchema())
186-
187-
selectedExample, err := getTypedExample(mediaType)
188-
assert.Nil(t, err)
189-
190-
selectedExampleJSON, err := json.Marshal(selectedExample)
191-
assert.Nil(t, err)
192-
assert.NotEmpty(t, string(selectedExampleJSON))
193-
td.validateResult(t, string(selectedExampleJSON))
194-
}
195-
}
196-
197-
func Test_GetTypedExampleShouldReturnErrorIfCannotGetFullExample(t *testing.T) {
198-
schema := openapi3.NewSchema()
199-
200-
parameterSchema1 := openapi3.NewStringSchema()
201-
parameterSchema1.Example = "testvalue"
202-
203-
parameterSchema2 := openapi3.NewObjectSchema()
204-
nestedParameterSchemaWithoutExample := openapi3.NewSchema()
205-
parameterSchema2.WithProperty("nestedProperty", nestedParameterSchemaWithoutExample)
206-
207-
schema.WithProperties(map[string]*openapi3.Schema{
208-
"name1": parameterSchema1,
209-
"name2": parameterSchema2,
210-
})
211-
212-
mediaType := openapi3.NewMediaType()
213-
mediaType.WithSchema(schema)
214-
215-
selectedExample, err := getTypedExample(mediaType)
216-
assert.NotNil(t, err)
217-
assert.Nil(t, selectedExample)
218-
}

example.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/getkin/kin-openapi/openapi3"
7+
)
8+
9+
// getTypedExampleFromSchema will return an example from a given schema
10+
func getTypedExampleFromSchema(schema *openapi3.Schema) (interface{}, error) {
11+
if schema.Example != nil {
12+
return schema.Example, nil
13+
}
14+
15+
if schema.Type == "number" {
16+
return 0, nil
17+
}
18+
if schema.Type == "integer" {
19+
return 0, nil
20+
}
21+
if schema.Type == "boolean" {
22+
return true, nil
23+
}
24+
if schema.Type == "string" {
25+
return "string", nil
26+
}
27+
if schema.Type == "array" {
28+
example := []interface{}{}
29+
if schema.Items != nil && schema.Items.Value != nil {
30+
ex, err := getTypedExampleFromSchema(schema.Items.Value)
31+
if err != nil {
32+
return nil, fmt.Errorf("can't get example for array item")
33+
}
34+
example = append(example, ex)
35+
}
36+
return example, nil
37+
}
38+
39+
if schema.Type == "object" || len(schema.Properties) > 0 {
40+
example := map[string]interface{}{}
41+
for k, v := range schema.Properties {
42+
ex, err := getTypedExampleFromSchema(v.Value)
43+
if err != nil {
44+
return nil, fmt.Errorf("can't get example for '%s'", k)
45+
}
46+
example[k] = ex
47+
}
48+
49+
if schema.AdditionalProperties != nil && schema.AdditionalProperties.Value != nil {
50+
addl := schema.AdditionalProperties.Value
51+
ex, err := getTypedExampleFromSchema(addl)
52+
if err != nil {
53+
return nil, fmt.Errorf("can't get example for additional properties")
54+
}
55+
56+
example["additionalPropertyName"] = ex
57+
}
58+
59+
return example, nil
60+
}
61+
62+
return nil, ErrNoExample
63+
}

0 commit comments

Comments
 (0)