Cb-tumblebug: Issue: Swagger with REST APIs that operate differently according to query param

Created on 7 Jul 2020  Β·  6Comments  Β·  Source: cloud-barista/cb-tumblebug

@seokho-son

예λ₯Ό λ“€μ–΄

[Call chain]

  1. POST /:nsId/resources/image (src/api/rest/server/server.go)
  2. func RestPostImage (src/api/rest/server/mcir/image.go)

이 κΈ°λŠ₯은

  1. POST /:nsId/resources/image?action=registerWithInfo 둜 호좜되면
    Request JSON body λ₯Ό TbImageInfo struct 둜 Bind (unmarshal) ν•˜κ³ 
  2. POST /:nsId/resources/image?action=registerWithId 둜 호좜되면
    Request JSON body λ₯Ό TbImageReq struct 둜 Bind (unmarshal) ν•©λ‹ˆλ‹€.

μ΄λŠ” func RestPostImage μ•ˆμ—μ„œ if 문으둜 κ΅¬ν˜„λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

    action := c.QueryParam("action")
    fmt.Println("[POST Image requested action: " + action)
    /*
        if action == "create" {
            fmt.Println("[Creating Image]")
            content, _ := createImage(nsId, u)
            return c.JSON(http.StatusCreated, content)

        } else */
    if action == "registerWithInfo" {
        fmt.Println("[Registering Image with info]")
        u := &mcir.TbImageInfo{}
        if err := c.Bind(u); err != nil {
            return err
        }
        content, err := mcir.RegisterImageWithInfo(nsId, u)
        if err != nil {
            common.CBLog.Error(err)
            mapA := map[string]string{"message": err.Error()}
            return c.JSON(http.StatusFailedDependency, &mapA)
        }
        return c.JSON(http.StatusCreated, content)
    } else if action == "registerWithId" {
        fmt.Println("[Registering Image with ID]")
        u := &mcir.TbImageReq{}
        if err := c.Bind(u); err != nil {
            return err
        }
        //content, responseCode, body, err := RegisterImageWithId(nsId, u)
        content, err := mcir.RegisterImageWithId(nsId, u)
        if err != nil {
            common.CBLog.Error(err)
            //fmt.Println("body: ", string(body))
            //return c.JSONBlob(responseCode, body)
            mapA := map[string]string{"message": err.Error()}
            return c.JSON(http.StatusFailedDependency, &mapA)
        }
        return c.JSON(http.StatusCreated, content)
    }

그런데 Swagger λ₯Ό μœ„ν•œ μ£Όμ„μ—λŠ”
ν•΄λ‹Ή λ‚΄μš© (Query param 에 따라 Request body 둜 μ‚¬μš©ν•  struct κ°€ 달라짐) 을 κΈ°μž¬ν•  μˆ˜κ°€ μ—†λŠ” κ²ƒμœΌλ‘œ λ³΄μž…λ‹ˆλ‹€.

...
// @Param registeringMethod query string true "registerWithInfo or registerWithId"
// @Param nsId path string true "Namespace ID"
// @Param imageInfo body mcir.TbImageInfo true "Details for an image object"
...

ν•΄κ²°μ±…μœΌλ‘œλŠ”

  • POST /:nsId/resources/image?action=registerWithInfo
  • POST /:nsId/resources/image?action=registerWithId

μ΄λ ‡κ²Œ κ΅¬λΆ„λ˜μ–΄ μžˆλŠ” 것을

  • POST /:nsId/resources/registerImageWithInfo
  • POST /:nsId/resources/registerImageWithId

이런 μ‹μœΌλ‘œ λΆ„λ¦¬ν•˜λŠ” μ•ˆμ΄ μžˆμŠ΅λ‹ˆλ‹€.
(REST API 의 URL 이 일반적인 REST API tradition κ³Ό λ§žμ§€ μ•ŠκΈ°λŠ” ν•©λ‹ˆλ‹€.)

All 6 comments

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.54. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@jihoon-seo μ•„λž˜μ™€ 같은 λ°©μ‹μœΌλ‘œ μ²˜λ¦¬ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?
λ‹€λ§Œ, response 도 쿼리 νŒŒλΌλ―Έν„°μ— 따라 λ‹€λ₯΄κ²Œ ν•˜λŠ” 것은 λ˜λŠ”μ§€ λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

// PostNs godoc
// @Summary Create namespace
// @Description Create namespace by json RestPostNs
// @Tags Namespace
// @Accept  json
// @Produce  json
(μš”κΈ°) // @Param registerWithInfo body common.NsInfo true "Post Ns2"
(μš”κΈ°) // @Param registerWithId body common.NsInfo true "Post Ns"
// @Success 200 {object} common.NsInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /ns [post]

image

@seokho-son
이 방법도 쒋은 것 κ°™μŠ΅λ‹ˆλ‹€.
μ΄λ ‡κ²Œ μ§„ν–‰ν•˜κ² μŠ΅λ‹ˆλ‹€.

echo-swagger 용 comment 에

// @Router /ns/{nsId}/resources/image?action={registeringMethod} [post]
μœ„μ™€ 같이 query param μ‚¬μš©λ²•μ„ λͺ…μ‹œμ μœΌλ‘œ 적어 μ£Όκ³  μ‹Άμ—ˆλŠ”λ°
μ΄λ ‡κ²Œ ν•˜λ©΄

swag i

2020/07/07 15:50:42 ParseComment error in file api/rest/server/mcir/image.go :can not parse router comment "/ns/{nsId}/resources/image?action={registeringMethod} [post]"

이런 μ—λŸ¬κ°€ λ‚©λ‹ˆλ‹€.


λ”°λΌμ„œ

// @Router /ns/{nsId}/resources/image [post]
μ΄λ ‡κ²Œ 적어야 ν•©λ‹ˆλ‹€. (query param μ‚¬μš©λ²•μ„ λͺ…μ‹œμ μœΌλ‘œ 적어 쀄 수 μ—†μŒ)

@jihoon-seo μ•„λž˜ 방식을 ν†΅ν•΄μ„œ μ²˜λ¦¬ν•  수 μžˆμ„ 것 κ°™μŠ΅λ‹ˆλ‹€.
https://github.com/cloud-barista/cb-tumblebug/discussions/414#discussioncomment-546987
mcis get action κ΄€λ ¨ API νŒŒλΌλ―Έν„°λŠ” μ²˜λ¦¬κ°€ 된 것 같은데,

MCIR μͺ½λ„ ν•œ 번 확인해봐주싀 수 μžˆμ„κΉŒμš”? μ²˜λ¦¬κ°€ λ˜μ–΄ μžˆλ‹€λ©΄ ~ ν•΄λ‹Ή μ΄μŠˆλŠ” close ν•˜λ©΄λ κ±° κ°™μŠ΅λ‹ˆλ‹€.. ^^

/close

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jihoon-seo picture jihoon-seo  Β·  3Comments

seokho-son picture seokho-son  Β·  3Comments

seokho-son picture seokho-son  Β·  5Comments

seokho-son picture seokho-son  Β·  5Comments

jihoon-seo picture jihoon-seo  Β·  4Comments