Skip to content

Commit f2d3767

Browse files
authored
docs: enhance JSON Schema documentation (#1455)
1 parent b4a23b5 commit f2d3767

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/website
2-
.idea/

core/json-schema.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,64 @@ docker-compose exec php \
2121
bin/console help api:json-schema:generate
2222
```
2323

24+
## Overriding the JSON Schema Specification
25+
26+
In a unit testing context, API Platform does not use the same schema version as the schema used when generating the API documentation. The version used by the documentation is the OpenAPI Schema version and the version used by unit testing is the JSON Schema version.
27+
28+
When [Testing the API](testing.md), JSON Schemas are useful to generate and automate unit testing. API Platform provides specific unit testing functionalities like [`assertMatchesResourceCollectionJsonSchema()`](testing.md#writing-functional-tests) or [`assertMatchesResourceItemJsonSchema()`](testing.md#writing-functional-tests) methods.
29+
These methods generate a JSON Schema then do unit testing based on the generated schema automatically.
30+
31+
Usually, the fact that API Platform uses a different schema version for unit testing is not a problem, but sometimes you may need to use the [`ApiProperty`](openapi.md#using-the-openapi-and-swagger-contexts) attribute to specify a [calculated field](serialization.md#calculated-field) type by overriding the OpenAPI Schema for the calculated field to be correctly documented.
32+
33+
When you will use [`assertMatchesResourceCollectionJsonSchema()`](testing.md#writing-functional-tests) or [`assertMatchesResourceItemJsonSchema()`](testing.md#writing-functional-tests) functions the unit test will fail on this [calculated field](serialization.md#calculated-field) as the unit testing process doesn't use the `openapi_context` you specified
34+
because API Platform is using the JSON Schema version instead at this moment.
35+
36+
So there is a way to override JSON Schema specification for a specific property in the JSON Schema used by the unit testing process.
37+
38+
You will need to add the `json_schema_context` property in the [`ApiProperty`](openapi.md#using-the-openapi-and-swagger-contexts) attribute to do this, example:
39+
40+
```php
41+
<?php
42+
43+
namespace App\Entity;
44+
45+
use ApiPlatform\Core\Annotation\ApiResource;
46+
use Doctrine\ORM\Mapping as ORM;
47+
48+
#[ORM\Entity]
49+
#[ApiResource]
50+
class Greeting
51+
{
52+
#[ORM\Id]
53+
#[ORM\GeneratedValue]
54+
#[ORM\Column(type: "integer")]
55+
private ?int $id = null;
56+
57+
// [...]
58+
59+
public function getId(): ?int
60+
{
61+
return $this->id;
62+
}
63+
64+
#[ApiProperty(attributes: [
65+
"openapi_context" => [
66+
"type" => "array",
67+
"items" => ["type" => "integer"]
68+
],
69+
"json_schema_context" => [ // <- MAGIC IS HERE, you can override the json_schema_context here.
70+
"type" => "array",
71+
"items" => ["type" => "integer"]
72+
]
73+
])]
74+
public function getSomeNumbers(): array {
75+
return [1, 2, 3, 4];
76+
}
77+
}
78+
```
79+
80+
You can obtain more information about the available [JSON Schema Types and format here](http://json-schema.org/understanding-json-schema/reference/type.html).
81+
2482
## Generating a JSON Schema Programmatically
2583

2684
To generate JSON Schemas programmatically, use the `api_platform.json_schema.schema_factory` [service](https://symfony.com/doc/current/service_container.html#fetching-and-using-services).

0 commit comments

Comments
 (0)