api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.45k stars 879 forks source link

Nullable type schema is not valid for openapi schema 3.x #6803

Closed nicolasne closed 1 week ago

nicolasne commented 1 week ago

API Platform version(s) affected: >= 3.2

Description

Since the 3.2 version, the openapi schema for nullable type is invalid for 3.x openapi schema.

How to reproduce

I use the Symfony api-platform version.

With the following ApiResource:

#[ApiResource()]
class MyApiRessource {
    private ?int $nullableAttribute = null;

    public function getNullableAttribute(): ?int
    {
        return $this->nullableAttribute;
    }

    public function setNullableAttribute(?int $nullableAttribute): MyApiRessource
    {
        $this->nullableAttribute = $nullableAttribute;
        return $this;
    }
}

And I generated the openapi schema with php bin/console api:openapi:export --spec-version=3.0.0

I got the following schema for #/components/schemas/MyApiRessource with api-plaftorm 3.1.22:

"MyApiRessource": {
 "type": "object",
 "description": "",
 "deprecated": false,
 "properties": {
     "nullableAttribute": {
         "type": "integer",
         "nullable": true
     }
 }
},

And for api-platform 3.2.0 (and upper):

"MyApiRessource": {
 "type": "object",
 "description": "",
 "deprecated": false,
 "properties": {
     "nullableAttribute": {
         "type": [
             "integer",
             "null"
         ]
     }
 }
},

It also concerns the application/ld+json, where hydra:mapping has a property that had this issue.

Possible Solution

According to the logs, it seems the bug has been introduce since https://github.com/api-platform/core/releases/tag/v3.2.0 and this specific pull request may introduce it https://github.com/api-platform/core/pull/5489.

soyuka commented 1 week ago

OpenAPI 3.1 is now based on JSON Schema 4 that uses multiple types instead of nullable. Use a custom normalizer if you need to change that although IIRC the spec-version can be used to keep the old schema

nicolasne commented 1 week ago

Thank for the answer.

I used the --spec-version to get the old schema (php bin/console api:openapi:export --spec-version=3.0.0 as I mentioned in my original message). This is why I created the issue in the first place.