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

Is it possible to bind uri and json for the same struct? #94

Open
chengr4 opened this issue Jun 13, 2023 · 7 comments
Open

Is it possible to bind uri and json for the same struct? #94

chengr4 opened this issue Jun 13, 2023 · 7 comments

Comments

@chengr4
Copy link

chengr4 commented Jun 13, 2023

Eg. for updateAccount API, How to bind uri and json for the same struct?

type updateAccountRequest struct {
    ID int64 `uri:"id" binding:"required,min=1"`
    Currency string `json:"currency" binding:"required,currency"`
}

func (server *Server) updateArticle(ctx *gin.Context) {
    var req updateAccountRequest
    if err := ctx.ShouldBindUri(&req); err != nil {
        // send 400 Bad Request to the client
        ctx.JSON(http.StatusBadRequest, errorResponse(err))
        return
    }

    if err := ctx.ShouldBindJSON(&req); err != nil {
        // send 400 Bad Request to the client
        ctx.JSON(http.StatusBadRequest, errorResponse(err))
        return
    }
}

the code above does not work.

Does anyone know? Thanks in advance

@chengr4
Copy link
Author

chengr4 commented Jun 20, 2023

I use result here: gin-gonic/gin#1824 (comment)
but I wonder whether there is a better solution

@TaylorDurden
Copy link

TaylorDurden commented Jul 17, 2024

I use result here: gin-gonic/gin#1824 (comment) but I wonder whether there is a better solution

type updateAccountRequest struct {
	ID      int64 `uri:"id" binding:"required,min=1"`
	Balance int64 `json:"balance"`
}

func (server *Server) updateAccountHandler(ctx *gin.Context) {
	var req updateAccountRequest
	if err := ctx.ShouldBindUri(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, errorResponse(err))
		return
	}

	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, errorResponse(err))
		return
	}
	arg := db.UpdateAccountParams{
		ID:      req.ID,
		Balance: req.Balance,
	}
	updatedAccount, err := server.store.UpdateAccount(ctx, arg)
	if err != nil {
		if err == sql.ErrNoRows {
			ctx.JSON(http.StatusNotFound, errorResponse(err))
			return
		}
		ctx.JSON(http.StatusInternalServerError, errorResponse(err))
		return
	}

	ctx.JSON(http.StatusOK, updatedAccount)
}

@chengr4
Copy link
Author

chengr4 commented Jul 18, 2024

@TaylorDurden thanks for help. However, if I recall correctly, this may not work.

@TaylorDurden
Copy link

@TaylorDurden thanks for help. However, if I recall correctly, this may not work.

@TaylorDurden thanks for help. However, if I recall correctly, this may not work.

TaylorDurden/go-simple-bank#4

You can try it in my PR branch. Maybe the new version gin resolved this.

@TaylorDurden
Copy link

TaylorDurden commented Jul 21, 2024

@chengr4
I guess that maybe you were testing a 400 Bad Request to the API client without currency field.
If you mark the currency field not required, this field won't be validated for 1 struct.

// this required binding will fail ctx.ShouldBindUri validation
Currency string `json:"currency" binding:"required,currency"` 

// this code will validate the struct fileds, if you did not pass the Currency field
ctx.ShouldBindUri(&req)  

FYI:
https://github.com/gin-gonic/gin/blob/cc4e11438cd6c0bcc632fe3492d3817dfa21c337/context.go#L763
https://github.com/gin-gonic/gin/blob/cc4e11438cd6c0bcc632fe3492d3817dfa21c337/binding/uri.go#L17

@chengr4
Copy link
Author

chengr4 commented Jul 22, 2024

@TaylorDurden But I usually want to validate it

@TaylorDurden
Copy link

TaylorDurden commented Jul 25, 2024

@TaylorDurden But I usually want to validate it

Then you should use another struct for this param...since it is from request body and use ShouldBindJSON to parse & validate it.
Although it is not a clean solution but it works as you metioned above...

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