Fiber: 馃 invalid response with nested struct

Created on 2 Oct 2020  路  5Comments  路  Source: gofiber/fiber

Hi guys

There are two structs (service belongs to request):

package Models

type Service struct {
    Id int `json:"id"`
    DomainId int `json:"domain_id"`
    Address string `json:"address"`
    Hint string `json:"hint"`
    City string `json:"city"`
    Latitude float64 `json:"latitude"`
    Longitude float64 `json:"longitude"`
    RequestId int `json:"request_id"`
    Request Request
}

and

package Models

type Request struct {
    Id int `json:"id"`
    So string `json:"so"`
    UserEmail string `json:"user_email"`
    Lang string `json:"lang"`
}

Now, for the find method

var service Models.Service
db.Find(&service , 1)

the resulset is a service row, but using fiber 2.0.4 with Json method from fiber.Ctx returns this:

{
    "id": 1,
    "domain_id": 1,
    "address": "401 Biscayne Blvd",
    "hint": "Bayside Marketplace",
    "city": "Miami",
    "latitude": 25.778333,
    "longitude": -80.186771,
    "request_id": 1,
    "request": {
        "id": 0,
        "so": "",
        "user_email": "",
        "lang": ""
    }
}

Expected answer

is there an output serializer or how can I solve this?

Thank you very much for the help 馃憤

tested with github.com/jinzhu/gorm and gorm.io/gorm in fiber 2.0.4 for both cases
```

馃 Question

Most helpful comment

Could you try

    // ..
    Longitude float64 `json:"longitude"`
    RequestId int     `json:"request_id"`
    Request Request   `json:"request"`
}

All 5 comments

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Could you try

    // ..
    Longitude float64 `json:"longitude"`
    RequestId int     `json:"request_id"`
    Request Request   `json:"request"`
}

The JSON serializing seems to work fine, so I'm sure the problem is somewhere in your DB lookup.

image

type Service struct {
    Id        int     `json:"id"`
    DomainId  int     `json:"domain_id"`
    Address   string  `json:"address"`
    Hint      string  `json:"hint"`
    City      string  `json:"city"`
    Latitude  float64 `json:"latitude"`
    Longitude float64 `json:"longitude"`
    RequestId int     `json:"request_id"`
    Request   Request `json:"request"`
}

type Request struct {
    Id        int    `json:"id"`
    So        string `json:"so"`
    UserEmail string `json:"user_email"`
    Lang      string `json:"lang"`
}

func main() {
    app := fiber.New()
    demo := Service{
        Id:        123,
        DomainId:  123,
        Address:   "demo",
        Hint:      "demo",
        City:      "demo",
        Latitude:  123.123,
        Longitude: 123.123,
        RequestId: 123,
        Request: Request{
            Id:        123,
            So:        "demo",
            UserEmail: "demo",
            Lang:      "demo",
        },
    }
    app.Get("/", func(c *fiber.Ctx) error {
        return c.JSON(demo)
    })
    log.Fatal(app.Listen(":3000"))
}

Feel free to re-open this issue 馃憤

Could you try

  // ..
  Longitude float64 `json:"longitude"`
  RequestId int     `json:"request_id"`
  Request Request   `json:"request"`
}

this way the same returns

By example in gorm

var service Models.Service
db.Find(&service , 1)

I need the answer when doing the find to be only:

{ "id": 1, "domain_id": 1, "address": "401 Biscayne Blvd", "hint": "Bayside Marketplace", "city": "Miami", "latitude": 25.778333, "longitude": -80.186771, "request_id": 1 }

Oh you are looking for the omitempty tag

    // ..
    Longitude float64 `json:"longitude"`
    RequestId int     `json:"request_id"`
    Request Request   `json:"request,omitempty"`
}

Or ignore the field completely

    // ..
    Longitude float64 `json:"longitude"`
    RequestId int     `json:"request_id"`
    Request Request   `json:"-"`
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

renanbastos93 picture renanbastos93  路  3Comments

Terisback picture Terisback  路  3Comments

bashery picture bashery  路  4Comments

jeyraj picture jeyraj  路  4Comments

mhf-ir picture mhf-ir  路  3Comments