diff --git a/example/generate.go b/example/generate.go index be25825..4d791ac 100644 --- a/example/generate.go +++ b/example/generate.go @@ -1,3 +1,3 @@ package example -//go:generate goctl api plugin -plugin goctl-swagger="swagger" -api user.api -dir . +//go:generate goctl api plugin -plugin goctl-swagger="swagger" -api test.api -dir . diff --git a/example/swagger.json b/example/swagger.json deleted file mode 100644 index e117ee7..0000000 --- a/example/swagger.json +++ /dev/null @@ -1,226 +0,0 @@ -{ - "swagger": "2.0", - "info": { - "title": "", - "version": "" - }, - "schemes": [ - "http", - "https" - ], - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "paths": { - "/api/user/:id": { - "get": { - "summary": "获取用户信息", - "operationId": "/api/user/:id", - "responses": { - "200": { - "description": "A successful response.", - "schema": { - "$ref": "#/definitions/UserInfoReply" - } - } - }, - "parameters": [ - { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/UserInfoReq" - } - } - ], - "tags": [ - "user-api" - ] - } - }, - "/api/user/login": { - "post": { - "summary": "登录", - "operationId": "/api/user/login", - "responses": { - "200": { - "description": "A successful response.", - "schema": {} - } - }, - "parameters": [ - { - "name": "body", - "in": "body", - "required": true, - "schema": {} - } - ], - "tags": [ - "user-api" - ] - } - }, - "/api/user/register": { - "post": { - "summary": "注册", - "description": "注册一个用户", - "operationId": "/api/user/register", - "responses": { - "200": { - "description": "A successful response.", - "schema": {} - } - }, - "parameters": [ - { - "name": "body", - "in": "body", - "required": true, - "schema": {} - } - ], - "tags": [ - "user-api" - ] - } - }, - "/api/user/search": { - "get": { - "summary": "用户搜索", - "operationId": "/api/user/search", - "responses": { - "200": { - "description": "A successful response.", - "schema": { - "$ref": "#/definitions/UserSearchReply" - } - } - }, - "parameters": [ - { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/UserSearchReq" - } - } - ], - "tags": [ - "user-api" - ] - } - }, - "/user/ping": { - "get": { - "operationId": "/user/ping", - "responses": { - "200": { - "description": "A successful response.", - "schema": {} - } - }, - "parameters": [ - { - "name": "body", - "in": "body", - "required": true, - "schema": {} - } - ], - "tags": [ - "greet" - ] - } - } - }, - "definitions": { - "LoginReq": { - "type": "object", - "properties": { - "Username": { - "type": "string" - }, - "Password": { - "type": "string" - } - }, - "title": "LoginReq" - }, - "RegisterReq": { - "type": "object", - "properties": { - "Username": { - "type": "string" - }, - "Password": { - "type": "string" - }, - "Mobile": { - "type": "string" - } - }, - "title": "RegisterReq" - }, - "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" - }, - "UserInfoReq": { - "type": "object", - "properties": { - "Id": { - "type": "string" - } - }, - "title": "UserInfoReq" - }, - "UserSearchReply": { - "type": "object", - "properties": { - "KeyWord": { - "type": "array", - "items": { - "$ref": "#/definitions/UserInfoReply" - } - } - }, - "title": "UserSearchReply" - }, - "UserSearchReq": { - "type": "object", - "properties": { - "KeyWord": { - "type": "string" - } - }, - "title": "UserSearchReq" - } - } -} diff --git a/example/test.api b/example/test.api new file mode 100644 index 0000000..18b49e9 --- /dev/null +++ b/example/test.api @@ -0,0 +1,49 @@ +info( + title: "小程序商城" + desc: "小程序商品首页" + version: "1.0" +) + + +type IndexResp struct { + NewList []NewList `json:"NewList"` + RandomList []RandomList `json:"Randomlist"` + BannerList []BannerList `json:"bannerlist"` +} +type NewList struct { + GoodsList +} +type RandomList struct { + GoodsList +} +type BannerList struct { + Image string `json:"image"` + Title string `json:"title"` + ID int64 `json:"id"` +} +type GoodsList { + GoodsID int64 `json:"goods_id"` + GoodsName string `json:"goods_name"` + CategoryID int64 `json:"category_id"` + Images string `json:"images"` + SpecType string `json:"spec_type"` + DeductStockType string `json:"deduct_stock_type"` + Content string `json:"content"` + SalesInitial int `json:"sales_initial"` + SalesActual int `json:"sales_actual"` + GoodsSort int `json:"goods_sort"` + DeliveryID int64 `json:"delivery_id"` + GoodsStatus string `json:"goods_status"` + IsDelete string `json:"is_delete"` + ImageFrist string `json:"ImageFrist"` + GoodsSales int `json:"goods_sales"` +} +service index-api { + @doc( + summary: "首页列表" + ) + @server( + handler: IndexHandler + ) + get /index/index returns (IndexResp) +} diff --git a/example/user.api b/example/user.api index 13dfce6..1eb14ee 100644 --- a/example/user.api +++ b/example/user.api @@ -75,5 +75,4 @@ service user-api { service user-api { @handler ping get /user/ping -} -//goctl api plugin -plugin goctl-swagger -api user.api -dir . \ No newline at end of file +} \ No newline at end of file diff --git a/generate/entities.go b/generate/entities.go index c4544f8..6915e07 100644 --- a/generate/entities.go +++ b/generate/entities.go @@ -21,6 +21,8 @@ var ( swaggerMapTypes = map[string]reflect.Kind{ "string": reflect.String, "int": reflect.Int, + "int32": reflect.Int, + "int64": reflect.Int64, "[]string": reflect.Slice, "bool": reflect.Bool, "struct": reflect.Struct, diff --git a/generate/parser.go b/generate/parser.go index 625b351..7908a12 100644 --- a/generate/parser.go +++ b/generate/parser.go @@ -45,21 +45,20 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge parameters := swaggerParametersObject{} reqRef := fmt.Sprintf("#/definitions/%s", route.RequestType.Name) - if len(route.ResponseType.Name) < 1 { - reqRef = "" - } - var schema = swaggerSchemaObject{ - schemaCore: schemaCore{ - Ref: reqRef, - }, + if len(route.RequestType.Name) > 0 { + var schema = swaggerSchemaObject{ + schemaCore: schemaCore{ + Ref: reqRef, + }, + } + parameters = append(parameters, swaggerParameterObject{ + Name: "body", + In: "body", + Required: true, + Schema: &schema, + }) } - parameters = append(parameters, swaggerParameterObject{ - Name: "body", - In: "body", - Required: true, - Schema: &schema, - }) pathItemObject, ok := paths[path] if !ok { pathItemObject = swaggerPathItemObject{} @@ -186,10 +185,13 @@ func schemaOfField(member spec.Member) swaggerSchemaObject { return ret } +// https://swagger.io/specification/ Data Types func primitiveSchema(kind reflect.Kind, t string) (ftype, format string, ok bool) { switch kind { case reflect.Int: return "integer", "int32", true + case reflect.Int64: + return "integer", "int64", true case reflect.Bool: return "boolean", "boolean", true case reflect.String: diff --git a/main.go b/main.go index db87302..78ea597 100644 --- a/main.go +++ b/main.go @@ -10,9 +10,9 @@ import ( ) var ( - version = "20210101" + version = "20210103" commands = []*cli.Command{ - { + {git add Name: "swagger", Usage: "generates swagger.json", Action: action.Generator,