diff --git a/example/user.api b/example/user.api index 9bdf38a..ad91057 100644 --- a/example/user.api +++ b/example/user.api @@ -18,7 +18,6 @@ type ( Username string `json:"username"` //测试 Password string `json:"password"`//测试2 } - UserInfoReq { Id string `path:"id"` } diff --git a/example/user.json b/example/user.json index b415115..69db4cc 100644 --- a/example/user.json +++ b/example/user.json @@ -16,6 +16,31 @@ "application/json" ], "paths": { + "/api/user/login": { + "post": { + "summary": "登录", + "operationId": "login", + "responses": { + "200": { + "description": "A successful response.", + "schema": {} + } + }, + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/LoginReq" + } + } + ], + "tags": [ + "user-api" + ] + } + }, "/api/user/register": { "post": { "summary": "注册", @@ -40,9 +65,78 @@ "user-api" ] } + }, + "/api/user/search": { + "get": { + "summary": "用户搜索", + "operationId": "searchUser", + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/UserInfoReply" + } + } + }, + "parameters": [ + { + "name": "keyWord", + "description": " 关键词", + "in": "query", + "required": true, + "type": "string" + } + ], + "tags": [ + "user-api" + ] + } + }, + "/api/user/{id}": { + "get": { + "summary": "获取用户信息", + "operationId": "getUserInfo", + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/UserInfoReply" + } + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "string" + } + ], + "tags": [ + "user-api" + ] + } } }, "definitions": { + "LoginReq": { + "type": "object", + "properties": { + "username": { + "type": "string", + "description": "测试" + }, + "password": { + "type": "string", + "description": "测试2" + } + }, + "title": "LoginReq", + "required": [ + "username", + "password" + ] + }, "RegisterReq": { "type": "object", "properties": { @@ -62,6 +156,63 @@ "password", "mobile" ] + }, + "UserInfoReply": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "age": { + "type": "integer", + "format": "int32" + }, + "birthday": { + "type": "string" + }, + "description": { + "type": "string" + }, + "tag": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "title": "UserInfoReply", + "required": [ + "name", + "age", + "birthday", + "description", + "tag" + ] + }, + "UserInfoReq": { + "type": "object", + "properties": { + "id": { + "type": "string" + } + }, + "title": "UserInfoReq", + "required": [ + "id" + ] + }, + "UserSearchReq": { + "type": "object", + "properties": { + "keyWord": { + "type": "string", + "description": " 关键词" + } + }, + "title": "UserSearchReq", + "required": [ + "keyWord" + ] } }, "securityDefinitions": { diff --git a/generate/parser.go b/generate/parser.go index d58fa2b..5d68c22 100644 --- a/generate/parser.go +++ b/generate/parser.go @@ -232,7 +232,9 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec. for _, tag := range member.Tags() { if len(tag.Options) == 0 { - schema.Required = append(schema.Required, tag.Name) + if !contains(schema.Required, tag.Name) && tag.Name != "required" { + schema.Required = append(schema.Required, tag.Name) + } continue } for _, option := range tag.Options {