swag icon indicating copy to clipboard operation
swag copied to clipboard

Method names for typescript are not generated according to the annotations when two routes have a similar prefix

Open srad opened this issue 1 year ago • 1 comments

Describe the bug Method names for typescript are not generated according to the annotations and their method names when more than one method uses the same path prefix. The example will make it clear, i.e. to generate a method name favPartialUpdate is will generate patchModel. The example will make it clear.

To Reproduce Steps to reproduce the behavior:

  1. Define two routes with the same prefix but different last path section, in my case:
apiv1.PATCH("/channels/:id/fav", v1.FavChannel)
apiv1.PATCH("/channels/:id/unfav", v1.UnFavChannel)
  1. The annotations for swager are:
// FavChannel godoc
// @Summary     Mark channel as favorite
// @Description Mark channel as favorite
// @Tags        channels
// @Accept      json
// @Produce     json
// @Param       id path uint true "Channel id"
// @Success     200 {} http.StatusOK
// @Failure     500 {} http.StatusInternalServerError
// @Router       /channels/{id}/fav [patch]
func FavChannel(c *gin.Context) {
  ...
}
// UnFavChannel godoc
// @Summary     Remove channel from favorite list
// @Description Remove channel from favorite list
// @Tags        channels
// @Accept      json
// @Produce     json
// @Param       id path uint true "Channel id"
// @Success     200 {} http.StatusOK
// @Failure     500 {} http.StatusInternalServerError
// @Router      /channels/{id}/unfav [patch]
func UnFavChannel(c *gin.Context) {
  ...
}
  1. Following typescript definitions are generated:
  patchChannels: (id: number, params: RequestParams = {}) =>
    this.http.request<any, any>({
      path: `/channels/${id}/fav`,
      method: "PATCH",
      type: ContentType.Json,
      format: "json",
      ...params,
    }),
  unfavPartialUpdate: (id: number, params: RequestParams = {}) =>
    this.http.request<any, any>({
      path: `/channels/${id}/unfav`,
      method: "PATCH",
      type: ContentType.Json,
      format: "json",
      ...params,
    }),

Expected behavior That the method should be favPartialUpdate and not patchChannels

Screenshots If applicable, add screenshots to help explain your problem.

Your swag version swag version v1.16.3

Your go version go version go1.22.4 linux/amd64

Desktop (please complete the following information):

  • Ubuntu 22.04

Additional context I fixed the issue with following templates. But I cannot really remember what I changed, because it's month ago. As far as I remember I changed the section of the code that analyses the route and generates the proper method name.

templates.zip

srad avatar Jul 24 '24 09:07 srad

To add to the last note, seem like I changed route-name.ejs:

const createCustomOperationId = (method, route, moduleName) => {
    const hasPathInserts = /\{(\w){1,}\}/g.test(route);
    const splitedRouteBySlash = _.compact(_.replace(route, /\{(\w){1,}\}/g, "").split("/"));

    const routeParts = (splitedRouteBySlash.length > 1
                    ? splitedRouteBySlash.splice(1)
                    : splitedRouteBySlash
    ).join("_");

    return methodAliases[method](routeParts, hasPathInserts);
};

srad avatar Jul 24 '24 09:07 srad