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

Bind JSON body request with nested slice of struct #3566

Closed
iamsubrataaich opened this issue Apr 11, 2023 · 5 comments
Closed

Bind JSON body request with nested slice of struct #3566

iamsubrataaich opened this issue Apr 11, 2023 · 5 comments

Comments

@iamsubrataaich
Copy link

Description

Trying to bind a struct with nested slice of struct. I have looked into following issues and tried them but still getting nil for the nested slice of struct.

How to reproduce

type Attribute struct {
	AttributeName string `json:"attributeName" bson:"attributeName" validate:"required" binding:"required"`
	Type          string `json:"type" bson:"type" validate:"required" binding:"required"`
	IsRequired    bool   `json:"isRequired" bson:"isRequired" validate:"required" binding:"required"`
	Min           int    `json:"min" bson:"min" validate:"required" `
	Max           int    `json:"max" bson:"max" validate:"required" `
}

type FamilyConfig struct {
	Name       string      `json:"name" bson:"name" validate:"required" binding:"required"`
	Attributes []Attribute `json:"attributes" bson:"attributes" validate:"dive" binding:"required,dive"`
}

func PostFamilyConfig(c *gin.Context) {

	var familyConfig FamilyConfig

	err := c.ShouldBind(&familyConfig)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Request data: %#v\n", familyConfig)


	c.JSON(http.StatusOK, familyConfig)
}

Expectations

$ curl --location 'localhost:8080/family-config' \
--header 'Content-Type: application/json' \
--data '{
    "name": "Toy",
    "attributes": [
        {
            "attributeName": "color",
            "type": "text",
            "isRequired": true,
            "min": 0,
            "max": 9
        },
        {
            "attributeName": "test",
            "type": "html",
            "isRequired": true,
            "min": 0,
            "max": 9
        }
    ]
}'

status: 200 Ok
{
    "name": "Toy",
    "attributes": [
        {
            "attributeName": "color",
            "type": "text",
            "isRequired": true,
            "min": 0,
            "max": 9
        },
        {
            "attributeName": "test",
            "type": "html",
            "isRequired": true,
            "min": 0,
            "max": 9
        }
    ]
}

Actual result

In the log.Printf I am getting nil for Attributes

$ Request data: familyConfigHandlers.FamilyConfig{Name:"lipstick", Attributes:[]*familyConfigHandlers.Attribute(nil)}

Environment

  • go version: 1.20
  • gin version (or commit ref): 1.9.0
  • operating system: Windows 10 Pro build: 19045.2728
@shplume
Copy link

shplume commented Apr 13, 2023

I just copy your code and run it, but the result is correct. You can see it below. And my environment is the same as yours. Maybe
you should look for an error somewhere else.

Result

image

image

@iamsubrataaich
Copy link
Author

iamsubrataaich commented Apr 13, 2023

I checked my code again and still getting nil. Did not find any error.

package main

import (
	"log"
	"net/http"

	"github.com/gin-gonic/gin"

)

type Attribute struct {
	AttributeName string `json:"attributeName" bson:"attributeName" validate:"required" binding:"required"`
	Type          string `json:"type" bson:"type" validate:"required" binding:"required"`
	IsRequired    bool   `json:"isRequired" bson:"isRequired" validate:"required" binding:"required"`
	Min           int    `json:"min" bson:"min" validate:"required" `
	Max           int    `json:"max" bson:"max" validate:"required" `
}

type FamilyConfig struct {
	Name       string      `json:"name" bson:"name" validate:"required" binding:"required"`
	Attributes []Attribute `json:"attributes" bson:"attributes" validate:"dive" binding:"dive"`
}

func main() {

	server := gin.Default()

	server.POST("/family-config", PostFamilyConfig)

	server.Run()
}

func PostFamilyConfig(c *gin.Context) {

	var r = c.Request.Body
	log.Print(r)

	var familyConfig FamilyConfig

	err := c.ShouldBindJSON(&familyConfig)
	if err != nil {
		c.AbortWithError(400, err)
		log.Fatal(err)
	}

	log.Printf("Request data: %#v\n", familyConfig)

	c.JSON(http.StatusOK, familyConfig)
}

@iamsubrataaich
Copy link
Author

I ran the application in a Ubuntu 22.04 virtual box and it is running as expected as @shplume said. I still can't find what is wrong with it when I am running it on my windows machine. I uninstalled Go and deleted the go folder then installed go again and tried. But still getting the error.

@shplume
Copy link

shplume commented Apr 14, 2023

I copied your code again and ran it on my windows machine. But the result was not changed, my environment is windows 10 Home build: 19045.2846. I‘m sorry that I can't help you more.

@sahinmurat
Copy link

sahinmurat commented Mar 5, 2024

you forget r in attRibute :). Your code is attibute not attribute :)

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

3 participants