You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/json-schema.md
+44-16Lines changed: 44 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,22 @@
1
1
# JSON Schema Support
2
2
3
-
[JSON Schema](https://json-schema.org/) is a popular vocabulary to describe the shape of JSON documents. A variant of JSON Schema is also used [in OpenAPI specifications](openapi.md).
3
+
[JSON Schema](https://json-schema.org/) is a popular vocabulary to describe the shape of JSON documents. A variant of JSON Schema is also used
4
+
[in OpenAPI specifications](openapi.md).
4
5
5
-
API Platform provides an infrastructure to generate JSON Schemas for any resource, represented in any format (including JSON-LD).
6
-
The generated schema can be used with libraries such as [react-json-schema-form](https://github.com/rjsf-team/react-jsonschema-form) to build forms for the documented resources, or to [be used for validation](https://json-schema.org/implementations.html#validators).
6
+
API Platform provides an infrastructure to generate JSON Schemas for any resource, represented in any format
7
+
(including JSON-LD).
8
+
The generated schema can be used with libraries such as [react-json-schema-form](https://github.com/rjsf-team/react-jsonschema-form) to build forms for the documented
9
+
resources, or to [be used for validation](https://json-schema.org/implementations.html#validators).
7
10
8
11
## Generating a JSON Schema
9
12
13
+
> [!WARNING]
14
+
> These commands are not yet available with Laravel, you're welcome to contribute [on GitHub](https://github.com/api-platform/core)
15
+
10
16
To export the schema corresponding to an API Resource, run the following command:
@@ -21,33 +27,50 @@ bin/console help api:json-schema:generate
21
27
22
28
## Overriding the JSON Schema Specification
23
29
24
-
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.
30
+
In a unit testing context, API Platform does not use the same schema version as the schema used when generating the API
31
+
documentation. The version used by the documentation is the OpenAPI Schema version and the version used by unit testing
32
+
is the JSON Schema version.
33
+
34
+
> [!NOTE]
35
+
> For assertions about JSON schemas in Laravel, refer to the
36
+
> [API Test Assertions in Laravel documentation](../laravel/testing.md#api-test-assertions-with-laravel).
25
37
26
-
When [Testing the API](../symfony/testing-utilities.md), JSON Schemas are useful to generate and automate unit testing. API Platform provides specific unit testing functionalities like [`assertMatchesResourceCollectionJsonSchema()`](../symfony/testing-utilities.md#writing-functional-tests) or [`assertMatchesResourceItemJsonSchema()`](../symfony/testing-utilities.md#writing-functional-tests) methods.
38
+
When [Testing the API](../core/testing.md), JSON Schemas are useful to generate and automate unit testing. API Platform provides specific
39
+
unit testing functionalities like [`assertMatchesResourceCollectionJsonSchema()`](../symfony/testing.md#writing-functional-tests) or
These methods generate a JSON Schema then do unit testing based on the generated schema automatically.
28
42
29
-
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.
43
+
Usually, the fact that API Platform uses a different schema version for unit testing is not a problem, but sometimes you
44
+
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
45
+
for the calculated field to be correctly documented.
30
46
31
-
When you will use [`assertMatchesResourceCollectionJsonSchema()`](../symfony/testing-utilities.md#writing-functional-tests) or [`assertMatchesResourceItemJsonSchema()`](../symfony/testing-utilities.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
32
-
because API Platform is using the JSON Schema version instead at this moment.
47
+
When you will use [`assertMatchesResourceCollectionJsonSchema()`](../symfony/testing.md#writing-functional-tests) or
48
+
[`assertMatchesResourceItemJsonSchema()`](../symfony/testing.md#writing-functional-tests) functions the unit test will
49
+
fail on this [calculated field](serialization.md#calculated-field) as the unit testing process doesn't use the `openapi_context`you specified because
50
+
API Platform is using the JSON Schema version instead at this moment.
33
51
34
52
So there is a way to override JSON Schema specification for a specific property in the JSON Schema used by the unit testing process.
35
53
36
-
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:
54
+
You will need to add the `json_schema_context` property in the [`ApiProperty`](openapi.md#using-the-openapi-and-swagger-contexts)
55
+
attribute to do this, example:
37
56
38
57
```php
39
58
<?php
40
59
41
-
namespace App\Entity;
60
+
namespace App\ApiResource;
42
61
43
62
use ApiPlatform\Metadata\ApiResource;
44
-
use Doctrine\ORM\Mapping as ORM;
45
63
46
-
#[ORM\Entity]
47
64
#[ApiResource]
48
65
class Greeting
49
66
{
50
-
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
67
+
#[ApiProperty(
68
+
identifier: true,
69
+
openapiContext: [
70
+
'type' => 'integer',
71
+
'example' => 1
72
+
]
73
+
)]
51
74
private ?int $id = null;
52
75
53
76
// [...]
@@ -77,9 +100,14 @@ You can obtain more information about the available [JSON Schema Types and forma
77
100
78
101
## Generating a JSON Schema Programmatically
79
102
80
-
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).
103
+
To generate JSON Schemas programmatically, use the `api_platform.json_schema.schema_factory`.
104
+
105
+
For further information, please consult the following documentations:
106
+
107
+
-[Symfony: Fetching and Using Services](https://symfony.com/doc/current/service_container.html#fetching-and-using-services)
0 commit comments