Skip to content

JsonSchema doesn't handle nullable properties that are objects #4133

Closed
@wuchen90

Description

@wuchen90

API Platform version(s) affected: 2.6.3

Description
Let's assume we have this two models:

/**
 * @ApiResource
 */
class Cart
{
  protected string $id = 'an-identifier';

  protected ?ShippingAddress $shippingAddress = null;
}
class ShippingAddress
{
  protected string $street = 'street';
}

Note: Only Cart is registered as Api Platform resource, ShippingAddress is only an embedded object.

Currently, the generated json schema looks like this:

 {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$ref": "#/definitions/Cart.jsonld",
    "definitions": {
        "Cart.jsonld": {
            "type": "object",
            "properties": {
                "@context": {
                    "readOnly": true,
                    "type": "string"
                },
                "@id": {
                    "readOnly": true,
                    "type": "string"
                },
                "@type": {
                    "readOnly": true,
                    "type": "string"
                },
                "id": {
                    "readOnly": true,
                    "type": "string",
                    "format": "uuid"
                },
                "shippingAddress": {
                    "nullable": true,
                    "anyOf": [
                        {
                            "$ref": "#/definitions/ShippingAddress.jsonld"
                        }
                    ]
                }
            }
        },
        "ShippingAddress.jsonld": {
            "type": "object",
            "properties": {
                "@context": {
                    "readOnly": true,
                    "type": "string"
                },
                "@id": {
                    "readOnly": true,
                    "type": "string"
                },
                "@type": {
                    "readOnly": true,
                    "type": "string"
                },
                "street": {
                    "type": "string"
                }
            }
        }
    }
}

The Cart.shippingAddress part is not a valid json schema, it must be:

                "shippingAddress": {
                    "type": ["object", "null"],
                    "anyOf": [
                        {
                            "$ref": "#/definitions/ShippingAddress.jsonld"
                        }
                    ]
                }

How to reproduce
Create the two models above and run php bin/console api:json-schema:generate 'Cart' to get the Json Schema.

Additional Context
The unit test for

public function getType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array

public function testGetType(array $schema, Type $type): void
{
$typeFactory = new TypeFactory();
$this->assertEquals($schema, $typeFactory->getType($type, 'json', null, null, new Schema(Schema::VERSION_OPENAPI)));
}

is twisted because it doesn't test by providing the value of $readableLink, which is true in our case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions