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

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

Open
srad opened this issue Jul 24, 2024 · 1 comment

Comments

@srad
Copy link

srad commented Jul 24, 2024

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
Copy link
Author

srad commented Jul 24, 2024

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);
};

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

1 participant