Skip to content

Commit df90765

Browse files
committedNov 14, 2023
openapi
1 parent e53b3ac commit df90765

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed
 

‎srv/codegen_apistuff.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,25 @@ func codegenOpenApi(apiRefl *apiReflect) (didFsWrites []string) {
230230
},
231231
ReqBody: yopenapi.ReqBody{
232232
Required: true,
233-
Descr: "Type name auto-suggestion: `" + method.In + "`",
233+
Descr: "Internal type ident: `" + method.In + "`",
234234
Content: map[string]yopenapi.Media{apisContentType: {Example: dummy_arg}},
235235
},
236236
Responses: map[string]yopenapi.Resp{
237237
"200": {
238-
Descr: method.Out,
238+
Descr: "Internal type ident: `" + method.Out + "`",
239239
Content: map[string]yopenapi.Media{apisContentType: {Example: dummy_ret}},
240240
Headers: map[string]yopenapi.Header{
241241
yoctx.HttpResponseHeaderName_UserId: {Descr: "0 if not authenticated, else current user's ID", Content: map[string]yopenapi.Media{"text/plain": {Example: "123"}}},
242242
},
243243
},
244244
},
245245
}}
246+
for http_status_code, errs := range sl.Grouped(api_method.KnownErrs(), func(it Err) string { return str.FromInt(it.HttpStatusCodeOr(500)) }) {
247+
path.Post.Responses[http_status_code] = yopenapi.Resp{
248+
Descr: "foo",
249+
Content: map[string]yopenapi.Media{"text/plain": {Examples: kv.FromKeys(errs, func(it Err) yopenapi.Example { return yopenapi.Example{Value: it} })}},
250+
}
251+
}
246252
openapi.Paths["/"+method.Path] = path
247253
}
248254

‎srv/openapi/openapi.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type ReqBody struct {
6060

6161
type Resp struct {
6262
Descr string `json:"description"`
63-
Headers map[string]Header `json:"headers"`
63+
Headers map[string]Header `json:"headers,omitempty"`
6464
Content map[string]Media `json:"content"`
6565
}
6666

@@ -72,7 +72,14 @@ type Header struct {
7272
}
7373

7474
type Media struct {
75-
Example any `json:"example"`
75+
Example any `json:"example,omitempty"`
76+
Examples map[string]Example `json:"examples,omitempty"`
77+
}
78+
79+
type Example struct {
80+
Summary string `json:"summary,omitempty"`
81+
Descr string `json:"description,omitempty"`
82+
Value any `json:"value"`
7683
}
7784

7885
var tyTime = reflect.TypeOf(time.Time{})

‎util/err.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var errSubstrToHttpStatusCode = map[string]int{
2828

2929
func (me Err) Error() string { return string(me) }
3030
func (me Err) String() string { return string(me) }
31+
func (me Err) AsAny() any { return me }
3132
func (me Err) HttpStatusCodeOr(preferredDefault int) int {
3233
for substr, code := range errSubstrToHttpStatusCode {
3334
if str.Has(string(me), substr) {

‎util/kv/kv.go

+16
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,19 @@ func Fill[K comparable, V any](dst map[K]V, from map[K]V) map[K]V {
1717
}
1818
return dst
1919
}
20+
21+
func FromKeys[K comparable, V any](keys []K, value func(K) V) map[K]V {
22+
ret := make(map[K]V, len(keys))
23+
for _, key := range keys {
24+
ret[key] = value(key)
25+
}
26+
return ret
27+
}
28+
29+
func FromValues[K comparable, V any](values []V, key func(V) K) map[K]V {
30+
ret := make(map[K]V, len(values))
31+
for _, val := range values {
32+
ret[key(val)] = val
33+
}
34+
return ret
35+
}

‎util/sl/sl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ func FirstNonNil[T any](slice ...*T) *T {
200200
return nil
201201
}
202202

203-
func Grouped[TKey comparable, TItem any, TSlice ~[]TItem](slice TSlice, key func(TItem) TKey) (ret map[TKey]TSlice) {
204-
ret = make(map[TKey]TSlice, len(slice)/2)
203+
func Grouped[TKey comparable, TItem any](slice Of[TItem], key func(TItem) TKey) (ret map[TKey]Of[TItem]) {
204+
ret = make(map[TKey]Of[TItem], len(slice)/2)
205205
for i := range slice {
206206
key := key(slice[i])
207207
ret[key] = append(ret[key], slice[i])

0 commit comments

Comments
 (0)