Yii2: XmlResponseFormatter outputs "false" as empty string

Created on 27 Oct 2016  路  8Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

Use boolean value in return from a rest controller.

return [
    'success' => false,
    'data' => [
        'reason' => 'No authentication configured.',
    ]
];

What is the expected result?

<response>
    <success>0</success>
    <data>
        <reason>No authentication configured.</reason>
    </data>
</response>

What do you get instead?

In case of true value I see 1 returned. I expected to see 0 returned for false. Would it be more correct or empty value is correct enough?

I understand that PHP itself converts false to an empty string. I'm trying to question from the XML consumer side what is the desired output format.

<response>
    <success></success>
    <data>
        <reason>No authentication configured.</reason>
    </data>
</response>

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.x |
| PHP version | any |
| Operating system | any |

ready for adoption bug

Most helpful comment

with respect to https://www.w3.org/TR/xmlschema11-2/#boolean, the following options are prescribed:

  • 'true' | 'false' | '1' | '0'

with respect to readibility and ambiguity, I would choose for true or false, since the return of 1 in consoles actually mean an error.

All 8 comments

Whats about something like

$child->appendChild(new DOMText((string) ($value === false ? '0' : $value)));

here: ?https://github.com/yiisoft/yii2/blob/333c4b6b4f4c3d33a30c8380d2da3eaadf08cb59/framework/web/XmlResponseFormatter.php#L94

Sounds OK to me. @yiisoft/core-developers ?

with respect to https://www.w3.org/TR/xmlschema11-2/#boolean, the following options are prescribed:

  • 'true' | 'false' | '1' | '0'

with respect to readibility and ambiguity, I would choose for true or false, since the return of 1 in consoles actually mean an error.

@nanodesu88 correct place

I would like to see 'true' and 'false' too. Thats what I actually expected :)
Should be very small fix!

if ($value === true) {
    $value = 'true';
} elseif ($value === false) {
    $value = 'false';
}
$child->appendChild(new DOMText((string) $value);

^ nvm, i am just testing some github behaviors

Fixed.

Was this page helpful?
0 / 5 - 0 ratings