fractal icon indicating copy to clipboard operation
fractal copied to clipboard

Allow at least stdClass objects as the return values from transformers

Open xrstf opened this issue 9 years ago • 15 comments

Right now, I'm forced to return an array from my transformer, no matter how hard I try to override classes in Fractal (there's no interface for serializers and the base class required everywhere in Fractal type-hints the arguments to array).

This is unfortunate because my API sometimes has objects that are -- when transformed -- an empty JSON object. But with Fractal, I'm forced to output [] instead of {}. While this is harmless in PHP by nature, this can be an issue in other languages.

It's also highly confusing when using embedding and doing sth like /thing?embed=child (so I'm embedding a single item that can be empty) and my JSON now reads

{
  "id": "thingID",
  "child": {
    "data": [ ]
  }
}

which implies that data normally is an array, even though when there is a child, the JSON looks like

{
  "id": "thingID",
  "child": {
    "data": {
      "id": "childID"
    }
  }
}

Also, just for aesthetic reasons, it would be nice to at least be able to return an new stdClass(); and get an empty object in my JSON.

xrstf avatar Jun 27 '15 00:06 xrstf

I think you raise a valid point here, I'll look into this tonight and see if I can get something done this week!

willishq avatar Aug 31 '15 13:08 willishq

On first look it appears that this might be more tricky than first seems. converting the data to JSON first calls toArray which is why empty data is parsed as an array.

The only way around this is to refactor the toJson method which I am looking at now!

willishq avatar Aug 31 '15 18:08 willishq

+1

rskvazh avatar Dec 02 '15 14:12 rskvazh

+1

clslrns avatar Dec 02 '15 14:12 clslrns

+1

alexpts avatar Dec 02 '15 14:12 alexpts

+1

Should be able to output null at least

ferodss avatar Dec 03 '15 14:12 ferodss

+1

adrianolc avatar Dec 03 '15 14:12 adrianolc

You can get around this by using a Collection instead of a Item.

cqcwillard avatar Apr 20 '16 14:04 cqcwillard

any solution from this, i have the same issue here

fathur avatar Jan 10 '18 05:01 fathur

+1

jezzdk avatar Jan 31 '18 15:01 jezzdk

any solution from this?

ghost avatar Feb 07 '18 06:02 ghost

To have an empty object in your output you need to not return anything in your transformer method. For example: Imagine you have this in your constructor: $this->defaultIncludes = [ 'data' ]; And this in your transformer: return ["id" => $this->getId(), "data" => (object)[]] And the method:

public function includeData() {
  if ($condition === true) {
    return $this->item($data, new DataTransformer());
  } // ----> Here if condition is not met your data would be empty and return value is going to be void or null.

This will result in empty object in your JSON as you expected.

snaderiBC avatar Mar 15 '18 03:03 snaderiBC

Or just pass null in the transformer; Like this: return ["id" => $this->getId(), "data" => null]

snaderiBC avatar Mar 15 '18 04:03 snaderiBC

Hey @snaderiBC - is it the only solution now as well? Is there any other option available? Thanks in advance.

tibinvpaul avatar Nov 28 '18 16:11 tibinvpaul

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 16 '22 06:04 stale[bot]