@@ -6,6 +6,16 @@ import (
66 "github.com/getkin/kin-openapi/openapi3"
77)
88
9+ // Mode defines a mode of operation for example generation.
10+ type Mode int
11+
12+ const (
13+ // ModeRequest is for the request body (writes to the server)
14+ ModeRequest Mode = iota
15+ // ModeResponse is for the response body (reads from the server)
16+ ModeResponse
17+ )
18+
919func getSchemaExample (schema * openapi3.Schema ) (interface {}, bool ) {
1020 if schema .Example != nil {
1121 return schema .Example , true
@@ -66,10 +76,26 @@ func stringFormatExample(format string) string {
6676 return ""
6777}
6878
79+ // excludeFromMode will exclude a schema if the mode is request and the schema
80+ // is read-only, or if the mode is response and the schema is write only.
81+ func excludeFromMode (mode Mode , schema * openapi3.Schema ) bool {
82+ if schema == nil {
83+ return true
84+ }
85+
86+ if mode == ModeRequest && schema .ReadOnly {
87+ return true
88+ } else if mode == ModeResponse && schema .WriteOnly {
89+ return true
90+ }
91+
92+ return false
93+ }
94+
6995// OpenAPIExample creates an example structure from an OpenAPI 3 schema
7096// object, which is an extended subset of JSON Schema.
7197// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#schemaObject
72- func OpenAPIExample (schema * openapi3.Schema ) (interface {}, error ) {
98+ func OpenAPIExample (mode Mode , schema * openapi3.Schema ) (interface {}, error ) {
7399 if ex , ok := getSchemaExample (schema ); ok {
74100 return ex , nil
75101 }
@@ -133,7 +159,7 @@ func OpenAPIExample(schema *openapi3.Schema) (interface{}, error) {
133159 example := []interface {}{}
134160
135161 if schema .Items != nil && schema .Items .Value != nil {
136- ex , err := OpenAPIExample (schema .Items .Value )
162+ ex , err := OpenAPIExample (mode , schema .Items .Value )
137163 if err != nil {
138164 return nil , fmt .Errorf ("can't get example for array item" )
139165 }
@@ -150,7 +176,11 @@ func OpenAPIExample(schema *openapi3.Schema) (interface{}, error) {
150176 example := map [string ]interface {}{}
151177
152178 for k , v := range schema .Properties {
153- ex , err := OpenAPIExample (v .Value )
179+ if excludeFromMode (mode , v .Value ) {
180+ continue
181+ }
182+
183+ ex , err := OpenAPIExample (mode , v .Value )
154184 if err != nil {
155185 return nil , fmt .Errorf ("can't get example for '%s'" , k )
156186 }
@@ -160,12 +190,15 @@ func OpenAPIExample(schema *openapi3.Schema) (interface{}, error) {
160190
161191 if schema .AdditionalProperties != nil && schema .AdditionalProperties .Value != nil {
162192 addl := schema .AdditionalProperties .Value
163- ex , err := OpenAPIExample (addl )
164- if err != nil {
165- return nil , fmt .Errorf ("can't get example for additional properties" )
166- }
167193
168- example ["additionalPropertyName" ] = ex
194+ if ! excludeFromMode (mode , addl ) {
195+ ex , err := OpenAPIExample (mode , addl )
196+ if err != nil {
197+ return nil , fmt .Errorf ("can't get example for additional properties" )
198+ }
199+
200+ example ["additionalPropertyName" ] = ex
201+ }
169202 }
170203
171204 return example , nil
0 commit comments