Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate API Docs using external tool #2464

Closed
a0v0 opened this issue Sep 27, 2022 · 1 comment
Closed

Generate API Docs using external tool #2464

a0v0 opened this issue Sep 27, 2022 · 1 comment

Comments

@a0v0
Copy link
Contributor

a0v0 commented Sep 27, 2022

Problem

When generating api documentaion using the command goctl api go -api test.api. Broken json file is produced. Moreover wrong query, path param are mapped in the generated swagger file.

problem 1

Insomnia_IdQhFhP7yU

problem 2

a sample request struct in test.api file

GetPublicUserAccountRequest {
		UserId   string `json:"user_id,optional" validate:"uuid"` 
		Username string `josn:"username,optional"`                
	}

here is the generated swagger file. see the required field

...
"GetPublicUserAccountRequest": {
      "type": "object",
      "properties": {
        "user_id": {
          "type": "string",
          "description": " The user ID of the user."
        },
        "Username": {
          "type": "string",
          "description": " The username assosciated with the account. Min 5 characters, max 50 characters. Only lowercase alphanumeric characters allowed."
        }
      },
      "title": "GetPublicUserAccountRequest",
      "required": [
        "uuid"
      ]
    },
...

Solution

Use already mature tool https://github.com/swaggo/swag

How to integrate without breaking changes?

Step 1: Replace the older comment format with the new one

older:

service backbone {
@doc(
		summary: Update User Account
		description: Update the account of currently logged in user. An empty 200 response indicates account update was successful.
	)
	@handler updateUserAccount
	post /account/update(UpdateUserAccountRequest) returns (UpdateUserAccountResponse)
}

new format:

	// UpdateMedia godoc
	// @Summary      Update Media File Metadata
	// @Description  Update metadata assosiated the media file.
	// @Description  Returns empty 200 ok response if successful.
	// @Accept       json
	// @Produce      json
	// @Param        types.UpdateMediaRequest  body      types.UpdateMediaRequest  true  "Update Media Request"
	// @Success      200      {object}  types.UpdateMediaResponse
	// @Router       /media/update [post]
	@handler updateUserAccount
	post /account/update(UpdateUserAccountRequest) returns (UpdateUserAccountResponse)
	

Basically retain any comment above the @handler (including multiline) and put these comments handler function in generated handler files. See below

// UpdateMedia godoc
// @Summary      Update Media File Metadata
// @Description  Update metadata assosiated the media file.
// @Description  Returns empty 200 ok response if successful.
// @Accept       json
// @Produce      json
// @Param        types.UpdateMediaRequest  body      types.UpdateMediaRequest  true  "Update Media Request"
// @Success      200      {object}  types.UpdateMediaResponse
// @Router       /media/update [post]
func UpdateUserAccountHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UpdateUserAccountRequest

		// go-zero validation
		if err := httpx.Parse(r, &req); err != nil {
			result.ParamErrorResult(r, w, err)
			return
		}
		l := userAccount.NewUpdateUserAccountLogic(r.Context(), svcCtx)
		resp, err := l.UpdateUserAccount(&req)
		result.HttpResult(r, w, resp, err)
	}
}

Does main.go file needs to be changed?

Yes. First install https://github.com/swaggo/swag and run swag init to generate a docs folder. Now int the main.go file add this import line _ "<packagename>/docs".

To add title, email, other meta add these comments above the main() function call or reference the swaggo/swag package for usage

package main

// TODO write tests

import (
	"fmt"
	"log"

	_ "example.com/docs"

	"github.com/zeromicro/go-zero/rest"
)


// @title           API
// @version         1.0
// @description     This is a sample server celler server.
// @termsOfService  http://example.com/terms/

// @contact.name   API Support
// @contact.url    http://www.example.com/support
// @contact.email  support@example.com

// @host      localhost:8080
// @BasePath  /
func main() {
...

@kevwan hope to see this in next release.

Maybe #2326 needs to be fixed first.

@WqyJh
Copy link
Contributor

WqyJh commented Apr 15, 2023

I write an api generation plugin to solve this problem, see https://github.com/WqyJh/goctl-gogen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants