Skip to content

Commit a06a54b

Browse files
committed
feat(openapi): New 在初始化之后即添加了 EmptyResponseRef 空的返回对象
1 parent dffbdb2 commit a06a54b

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

locales/und.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ messages:
4343
- key: no available peer
4444
message:
4545
msg: no available peer
46+
- key: no response
47+
message:
48+
msg: no response
4649
- key: not found
4750
message:
4851
msg: not found

locales/zh.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ messages:
4545
- key: no available peer
4646
message:
4747
msg: 没有有效的节点
48+
- key: no response
49+
message:
50+
msg: 无返回内容
4851
- key: not found
4952
message:
5053
msg: 未找到

openapi/middleware.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ func (o *Operation) ResponseRef(status, ref string, summary, description web.Loc
305305
return o
306306
}
307307

308+
// Response200 相当于 o.Response("200", resp, nil, nil)
309+
func (o *Operation) Response200(resp any) *Operation {
310+
return o.Response("200", resp, nil, nil)
311+
}
312+
313+
// ResponseEmpty 相当于 o.ResponseRef(status, EmptyResponseRef, nil, nil)
314+
func (o *Operation) ResponseEmpty(status string) *Operation {
315+
return o.ResponseRef(status, EmptyResponseRef, nil, nil)
316+
}
317+
308318
// CallbackRef 引用 components 中定义的回调对象
309319
func (o *Operation) CallbackRef(name, ref string, summary, description web.LocaleStringer) *Operation {
310320
if _, found := o.d.components.callbacks[ref]; !found {

openapi/openapi.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ const (
9090
SecuritySchemeTypeOpenIDConnect = "openIdConnect"
9191
)
9292

93+
// 文档中表示没有返回对象在 components/responses 中的引用值
94+
const EmptyResponseRef = "empty-response-ref"
95+
9396
type (
9497
// Document openapi 文档
9598
Document struct {
@@ -431,6 +434,10 @@ func New(s web.Server, title web.LocaleStringer, o ...Option) *Document {
431434
s: s,
432435
}
433436

437+
o = append(o, WithResponse(&Response{
438+
Ref: &Ref{Ref: EmptyResponseRef},
439+
Description: web.Phrase("no response"),
440+
}))
434441
for _, opt := range o {
435442
opt(doc)
436443
}

openapi/option.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,6 @@ func WithProblemResponse() Option {
8787
}, "4XX", "5XX")
8888
}
8989

90-
// WithClassicResponse 提供框架一些常用的 [Response] 对象
91-
//
92-
// 包含了 4XX 和 5XX 的错误对象;
93-
// 包含了一个 ref 为 empty 的空对象;
94-
func WithClassicResponse() Option {
95-
return WithOptions(
96-
WithProblemResponse(),
97-
WithResponse(&Response{Ref: &Ref{Ref: "empty"}}),
98-
)
99-
}
100-
10190
// WithMediaType 指定所有接口可用的媒体类型
10291
//
10392
// t 用于指定支持的媒体类型,必须是 [web.Server] 实例支持的类型。

openapi/option_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func TestWithResponse(t *testing.T) {
6464
WithResponse(&Response{Ref: &Ref{Ref: "500"}}, "500"),
6565
)
6666
a.NotNil(d).
67-
Length(d.components.responses, 2).
67+
Length(d.components.responses, 3).
68+
Length(d.responses, 2).
6869
Equal(d.responses["400"], "400").
6970
Equal(d.responses["500"], "500")
7071
}
@@ -76,7 +77,7 @@ func TestWithProblemResponse(t *testing.T) {
7677
d := New(ss, web.Phrase("desc"), WithProblemResponse())
7778
a.NotNil(d).
7879
Length(d.responses, 2).
79-
Length(d.components.responses, 1).
80+
Length(d.components.responses, 2).
8081
Equal(d.responses["4XX"], "problem").
8182
Equal(d.responses["5XX"], "problem")
8283
}

server/config/CONFIG.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ <h2 id="httpConfig">httpConfig</h2>
160160
<tbody>
161161
<tr><td>port,omitempty</td><td>port,omitempty</td><td>port,attr,omitempty</td><td>string</td><td><p>端口
162162
<p>格式与 [http.Server.Addr] 相同。可以为空,表示由 [http.Server] 确定其默认值。
163+
</td></tr><tr><td>url,omitempty</td><td>url,omitempty</td><td>url,omitempty</td><td>string</td><td><p>[web.Router.URL] 的默认前缀
164+
<p>如果是非标准端口,应该带上端口号。
165+
<p>NOTE: 每个路由可使用 [web.WithURLDomain] 重新定义该值。
163166
</td></tr><tr><td>requestID,omitempty</td><td>requestID,omitempty</td><td>requestID,omitempty</td><td>string</td><td><p>x-request-id 的报头名称
164167
<p>如果为空,则采用 [header.XRequestID] 作为默认值。
165168
</td></tr><tr><td>certificates,omitempty</td><td>certificates,omitempty</td><td>certificates&gt;certificate,omitempty</td><td><a href="#certificateConfig">certificateConfig</a></td><td><p>网站的域名证书

0 commit comments

Comments
 (0)