diff --git a/.gitignore b/.gitignore index a7c0aba13bf..6a612f1a173 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ /website -.idea/ diff --git a/core/json-schema.md b/core/json-schema.md index 0dfc0e487ba..13bb81888d7 100644 --- a/core/json-schema.md +++ b/core/json-schema.md @@ -21,6 +21,64 @@ docker-compose exec php \ bin/console help api:json-schema:generate ``` +## Overriding the JSON Schema Specification + +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. + +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. +These methods generate a JSON Schema then do unit testing based on the generated schema automatically. + +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. + +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 +because API Platform is using the JSON Schema version instead at this moment. + +So there is a way to override JSON Schema specification for a specific property in the JSON Schema used by the unit testing process. + +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: + +```php +id; + } + + #[ApiProperty(attributes: [ + "openapi_context" => [ + "type" => "array", + "items" => ["type" => "integer"] + ], + "json_schema_context" => [ // <- MAGIC IS HERE, you can override the json_schema_context here. + "type" => "array", + "items" => ["type" => "integer"] + ] + ])] + public function getSomeNumbers(): array { + return [1, 2, 3, 4]; + } +} +``` + +You can obtain more information about the available [JSON Schema Types and format here](http://json-schema.org/understanding-json-schema/reference/type.html). + ## Generating a JSON Schema Programmatically 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).