labstack / echo

High performance, minimalist Go web framework
https://echo.labstack.com
MIT License
29.01k stars 2.21k forks source link

Omitempty in form #2612

Closed SimonAM closed 3 months ago

SimonAM commented 3 months ago

Issue Description

Cannot bind form-data with omitempty tag.

Checklist

Expected behaviour

Should bind form data with omitempty tag if the data exists

Actual behaviour

Field with omitempty tag does not get bound.

Steps to reproduce

See working code example below:

Working code to debug

package main

import (
    "net/http"

    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()

    e.POST("/issue", PostExample)

    e.Logger.Fatal(e.Start(":8080"))
}

type FormStruct struct {
    MyField    string `form:"myField,omitempty" json:"myField"`
    OtherField string `form:"otherField" json:"otherField"`
}

func PostExample(c echo.Context) error {
    var formData FormStruct
    _ = c.Bind(&formData)
    return c.JSON(http.StatusOK, formData)
}

Edit: And curl: curl -X POST http://localhost:8080/issue \ -d "myField=myFieldData" \ -d "otherField=otherFieldData" \ -H "Content-Type: application/x-www-form-urlencoded"

Version/commit

aldas commented 3 months ago

omitempty works only for json and xml sources as these use Go Std marshalling. form,query, param do not support it.