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
+58Lines changed: 58 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,64 @@ docker-compose exec php \
21
21
bin/console help api:json-schema:generate
22
22
```
23
23
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:
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
+
24
82
## Generating a JSON Schema Programmatically
25
83
26
84
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