Skip to content

Yaml: dump numeric keys as strings instead of numbers #48061

Closed
@mabar

Description

@mabar

Description

The OpenAPI specification currently requires all keys in Responses Object to be strings, as decribed here https://github.com/OAI/OpenAPI-Specification/blob/157a4c81ae537ef793b2bee368bc00d88b461de8/versions/3.0.2.md#patterned-fields-1

This includes values like '200', which are type-casted to int by PHP.

For further info see OAI/OpenAPI-Specification#3068

To work around that I would like to add a flag to encode numeric keys as strings in YAML. This solution should probably skip keys of auto-indexed arrays (with keys starting from 0)

Example

Instead of calling

use Symfony\Component\Yaml\Yaml;
Yaml::dump([200 => 'foo']);

and producing

200: 'foo'

Call

use Symfony\Component\Yaml\Yaml;
Yaml::dump([200 => 'foo'], flags: Yaml::DUMP_NUMERIC_KEY_AS_STRING);

and produce

'200': 'foo'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions