Skip to content

Commit

Permalink
fix: content type
Browse files Browse the repository at this point in the history
  • Loading branch information
EZ4Jam1n committed Aug 10, 2024
1 parent 97ee259 commit a32dd30
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 60 deletions.
50 changes: 25 additions & 25 deletions thrift-gen-http-swagger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

English | [中文](README_CN.md)

HTTP Swagger document generation plugin for cloudwego/cwgo & hertz.
HTTP Swagger document generation plugin for [cloudwego/cwgo](https://github.com/cloudwego/cwgo) & [hertz](https://github.com/cloudwego/hertz).

## Supported hz Annotations

Expand All @@ -14,15 +14,15 @@ HTTP Swagger document generation plugin for cloudwego/cwgo & hertz.

#### Annotation Explanation

| Annotation | Explanation |
|----------------|----------------------------------------------------------------------------------------|
| `api.query` | `api.query` corresponds to `parameter` with `in: query` |
| `api.path` | `api.path` corresponds to `parameter` with `in: path`, required is true |
| `api.header` | `api.header` corresponds to `parameter` with `in: header` |
| `api.cookie` | `api.cookie` corresponds to `parameter` with `in: cookie` |
| `api.body` | `api.body` corresponds to `requestBody` with `content`: `application/json` |
| `api.form` | `api.form` corresponds to `requestBody` with `content`: `multipart/form-data` |
| `api.raw_body` | `api.raw_body` corresponds to `requestBody` with `content`: `application/octet-stream` |
| Annotation | Explanation |
|----------------|----------------------------------------------------------------------------------------------------------------------|
| `api.query` | `api.query` corresponds to `parameter` with `in: query` |
| `api.path` | `api.path` corresponds to `parameter` with `in: path`, required is true |
| `api.header` | `api.header` corresponds to `parameter` with `in: header` |
| `api.cookie` | `api.cookie` corresponds to `parameter` with `in: cookie` |
| `api.body` | `api.body` corresponds to `requestBody` with `content`: `application/json` |
| `api.form` | `api.form` corresponds to `requestBody` with `content`: `multipart/form-data` or `application/x-www-form-urlencoded` |
| `api.raw_body` | `api.raw_body` corresponds to `requestBody` with `content`: `text/plain` |

### Response Specification

Expand All @@ -31,28 +31,28 @@ HTTP Swagger document generation plugin for cloudwego/cwgo & hertz.

#### Annotation Explanation

| Annotation | Explanation |
|----------------|-------------------------------------------------------------------------------------|
| `api.header` | `api.header` corresponds to `response` with `header` |
| `api.body` | `api.body` corresponds to `response` with `content`: `application/json` |
| `api.raw_body` | `api.raw_body` corresponds to `response` with `content`: `application/octet-stream` |
| Annotation | Explanation |
|----------------|-------------------------------------------------------------------------|
| `api.header` | `api.header` corresponds to `response` with `header` |
| `api.body` | `api.body` corresponds to `response` with `content`: `application/json` |
| `api.raw_body` | `api.raw_body` corresponds to `response` with `content`: `text/plain` |

### Method Specification

1. Each `method` is associated with a `pathItem` through an annotation.

#### Annotation Explanation

| Annotation | Explanation |
|---------------|---------------------------------------------------------------|
| `api.get` | `api.get` corresponds to GET request, only `parameters ` |
| `api.put` | `api.put` corresponds to PUT request |
| `api.post` | `api.post` corresponds to POST request |
| `api.patch` | `api.patch` corresponds to PATCH request |
| `api.delete` | `api.delete` corresponds to DELETE request, only `parameters` |
| `api.options` | `api.options` corresponds to OPTIONS request |
| `api.head` | `api.head` corresponds to HEAD request, only `parameters` |
| `api.baseurl` | `api.baseurl` corresponds to `server` `url` of `pathItem` |
| Annotation | Explanation |
|---------------|---------------------------------------------------------------------------------------------------|
| `api.get` | `api.get` corresponds to GET request, only `parameters ` |
| `api.put` | `api.put` corresponds to PUT request |
| `api.post` | `api.post` corresponds to POST request |
| `api.patch` | `api.patch` corresponds to PATCH request |
| `api.delete` | `api.delete` corresponds to DELETE request, only `parameters` |
| `api.options` | `api.options` corresponds to OPTIONS request |
| `api.head` | `api.head` corresponds to HEAD request, only `parameters` |
| `api.baseurl` | `api.baseurl` corresponds to `server` `url` of `pathItem`, This annotation is not supported by hz |

### Service Specification

Expand Down
50 changes: 25 additions & 25 deletions thrift-gen-http-swagger/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[English](README.md) | 中文

适用于 cloudwego/cwgo & hertz 的 http swagger 文档生成插件。
适用于 [cloudwego/cwgo](https://github.com/cloudwego/cwgo) & [hertz](https://github.com/cloudwego/hertz) 的 http swagger 文档生成插件。

## 支持的 hz 注解

Expand All @@ -14,15 +14,15 @@

#### 注解说明

| 注解 | 说明 |
|----------------|--------------------------------------------------------------------------|
| `api.query` | `api.query` 对应 `parameter``in: query` 参数 |
| `api.path` | `api.path` 对应 `parameter``in: path` 参数, `required``true` |
| `api.header` | `api.header` 对应 `parameter``in: header` 参数 |
| `api.cookie` | `api.cookie` 对应 `parameter``in: cookie` 参数 |
| `api.body` | `api.body` 对应 `requestBody``content``application/json` |
| `api.form` | `api.form` 对应 `requestBody``content``multipart/form-data` |
| `api.raw_body` | `api.raw_body` 对应 `requestBody``content``application/octet-stream` |
| 注解 | 说明 |
|----------------|-------------------------------------------------------------------------------------------------------|
| `api.query` | `api.query` 对应 `parameter``in: query` 参数 |
| `api.path` | `api.path` 对应 `parameter``in: path` 参数, `required``true` |
| `api.header` | `api.header` 对应 `parameter``in: header` 参数 |
| `api.cookie` | `api.cookie` 对应 `parameter``in: cookie` 参数 |
| `api.body` | `api.body` 对应 `requestBody``content``application/json` |
| `api.form` | `api.form` 对应 `requestBody``content``multipart/form-data` `application/x-www-form-urlencoded` |
| `api.raw_body` | `api.raw_body` 对应 `requestBody``content``text/plain` |

### Response 规范

Expand All @@ -31,28 +31,28 @@

#### 注解说明

| 注解 | 说明 |
|----------------|-----------------------------------------------------------------------|
| `api.header` | `api.header` 对应 `response``header` |
| `api.body` | `api.body` 对应 `response``content``application/json` |
| `api.raw_body` | `api.raw_body` 对应 `response``content``application/octet-stream` |
| 注解 | 说明 |
|----------------|-----------------------------------------------------------|
| `api.header` | `api.header` 对应 `response``header` |
| `api.body` | `api.body` 对应 `response``content``application/json` |
| `api.raw_body` | `api.raw_body` 对应 `response``content``text/plain` |

### Method 规范

1. 每个 `method` 通过注解来关联 `pathItem`

#### 注解说明

| 注解 | 说明 |
|---------------|------------------------------------------------|
| `api.get` | `api.get` 对应 `GET` 请求,只有 `parameter` |
| `api.put` | `api.put` 对应 `PUT` 请求 |
| `api.post` | `api.post` 对应 `POST` 请求 |
| `api.patch` | `api.patch` 对应 `PATCH` 请求 |
| `api.delete` | `api.delete` 对应 `DELETE` 请求,只有 `parameter` |
| `api.options` | `api.options` 对应 `OPTIONS` 请求 |
| `api.head` | `api.head` 对应 `HEAD` 请求,只有 `parameter` |
| `api.baseurl` | `api.baseurl` 对应 `pathItem``server``url` |
| 注解 | 说明 |
|---------------|---------------------------------------------------------|
| `api.get` | `api.get` 对应 `GET` 请求,只有 `parameter` |
| `api.put` | `api.put` 对应 `PUT` 请求 |
| `api.post` | `api.post` 对应 `POST` 请求 |
| `api.patch` | `api.patch` 对应 `PATCH` 请求 |
| `api.delete` | `api.delete` 对应 `DELETE` 请求,只有 `parameter` |
| `api.options` | `api.options` 对应 `OPTIONS` 请求 |
| `api.head` | `api.head` 对应 `HEAD` 请求,只有 `parameter` |
| `api.baseurl` | `api.baseurl` 对应 `pathItem``server``url`, 非hz支持注解 |

### Service 规范

Expand Down
30 changes: 22 additions & 8 deletions thrift-gen-http-swagger/example/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/HelloResp'
$ref: '#/components/schemas/HelloRespBody'
servers:
- url: http://127.0.0.1:8888
/form:
Expand All @@ -68,6 +68,20 @@ paths:
form3:
$ref: '#/components/schemas/InnerForm'
description: Hello - request
application/x-www-form-urlencoded:
schema:
title: Hello - request
required:
- form1
type: object
properties:
form1:
title: this is an override field schema title
maxLength: 255
type: string
form3:
$ref: '#/components/schemas/InnerForm'
description: Hello - request
responses:
"200":
description: HelloResp
Expand All @@ -78,7 +92,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/HelloResp'
$ref: '#/components/schemas/HelloRespBody'
servers:
- url: http://127.0.0.1:8888
/hello1:
Expand All @@ -105,7 +119,7 @@ paths:
- name: query1
in: query
description: |-
对于map类型调试时需要转义才能解析,如下所示
对于parameters中的map类型调试时需要转义才能解析,如下所示
{
"query1": "{\"key\":\"value\"}"
}
Expand All @@ -123,7 +137,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/HelloResp'
$ref: '#/components/schemas/HelloRespBody'
servers:
- url: http://127.0.0.1:8888
/hello2:
Expand Down Expand Up @@ -152,7 +166,7 @@ paths:
- name: query1
in: query
description: |-
对于map类型调试时需要转义才能解析,如下所示
对于parameters中的map类型调试时需要转义才能解析,如下所示
{
"query1": "{\"key\":\"value\"}"
}
Expand All @@ -170,7 +184,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/HelloResp'
$ref: '#/components/schemas/HelloRespBody'
servers:
- url: http://127.0.0.1:8889
/path{path1}:
Expand All @@ -195,12 +209,12 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/HelloResp'
$ref: '#/components/schemas/HelloRespBody'
servers:
- url: http://127.0.0.1:8888
components:
schemas:
HelloResp:
HelloRespBody:
title: Hello - response
required:
- body
Expand Down
13 changes: 11 additions & 2 deletions thrift-gen-http-swagger/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,20 @@ func (g *OpenAPIGenerator) buildOperation(
},
},
})

additionalProperties = append(additionalProperties, &openapi.NamedMediaType{
Name: "application/x-www-form-urlencoded",
Value: &openapi.MediaType{
Schema: &openapi.SchemaOrReference{
Schema: formSchema,
},
},
})
}

if len(rawBodySchema.Properties.AdditionalProperties) > 0 {
additionalProperties = append(additionalProperties, &openapi.NamedMediaType{
Name: "application/octet-stream",
Name: "text/plain",
Value: &openapi.MediaType{
Schema: &openapi.SchemaOrReference{
Schema: rawBodySchema,
Expand Down Expand Up @@ -594,7 +603,7 @@ func (g *OpenAPIGenerator) getResponseForStruct(d *openapi.Document, desc *thrif
ref := "#/components/schemas/" + desc.GetName() + "RawBody"
g.addSchemaToDocument(d, refSchema)
additionalProperties = append(additionalProperties, &openapi.NamedMediaType{
Name: "application/octet-stream",
Name: "text/plain",
Value: &openapi.MediaType{
Schema: &openapi.SchemaOrReference{
Reference: &openapi.Reference{Xref: ref},
Expand Down

0 comments on commit a32dd30

Please sign in to comment.