When returning relationships that are optional vapor returns an empty json element

I have a vapor route that return an EventLoopFuture of my type. This type has an @OptionalParent, which points to another Model. When the data comes back and this field is nil the returned json of the client caller has an empty "shell" object in place of the nil OptionalParent. Is there no way of just not returning anything for this parent when it is nil? This is my query.

        .query(on: req.db)
        .with(\.$parentComment) { parentComment in

This is to do with how the relationship property wrappers override Encodable to allow them to work when inserting data into the database. There is an open issue on GH to try and improve this, but you can transform your model into another representation (also called a DTO) which maps to the JSON you want to return

1 Like

Yeah that's what I thought. I just needed confirmation. Thank you.

Terms of Service

Privacy Policy

Cookie Policy