Skip to content

docs: Enhance Json schema documentation #1455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c25af50
Update json-schema.md
ES-Six Oct 28, 2021
32040ab
Update json-schema.md
ES-Six Oct 28, 2021
bedb8d5
Update json-schema.md
ES-Six Oct 28, 2021
e021b22
Update json-schema.md
ES-Six Oct 28, 2021
dd104fc
Create json-schema.md
ES-Six Oct 28, 2021
c946787
Update json-schema.md
ES-Six Oct 28, 2021
90ab137
Update json-schema.md
ES-Six Oct 28, 2021
916a711
Update json-schema.md
ES-Six Oct 28, 2021
c05a77e
Update json-schema.md
ES-Six Oct 28, 2021
2190441
Update json-schema.md
ES-Six Oct 28, 2021
fa3e76c
Update json-schema.md
ES-Six Oct 28, 2021
be92490
Update json-schema.md
ES-Six Oct 28, 2021
96a0d61
Update json-schema.md
ES-Six Oct 28, 2021
dad5174
Update json-schema.md
ES-Six Oct 28, 2021
1d45785
Update core/json-schema.md
ES-Six Oct 29, 2021
b85fdc7
Update core/json-schema.md
ES-Six Oct 29, 2021
b36a072
Update core/json-schema.md
ES-Six Oct 29, 2021
8b77a0c
Update core/json-schema.md
ES-Six Oct 29, 2021
79a7b59
Update core/json-schema.md
ES-Six Oct 29, 2021
d85f9ae
Update json-schema.md
ES-Six Oct 29, 2021
bca81bd
Update core/json-schema.md
ES-Six Oct 29, 2021
26e2831
Update core/json-schema.md
ES-Six Oct 29, 2021
1a03d16
Update core/json-schema.md
ES-Six Oct 29, 2021
a43a5a0
Prevent IDE files to be pushed
ES-Six Oct 30, 2021
89b0221
fix typo
ES-Six Oct 30, 2021
a7d11d7
Lint file
ES-Six Oct 30, 2021
1e3fe30
After prose lint improvements
ES-Six Oct 30, 2021
6fbe388
Merge branch 'api-platform:2.6' into json-schema-doc-enhancement
ES-Six Oct 30, 2021
0cecc60
Removing IDE files
ES-Six Nov 3, 2021
6919d2b
Merge branch 'api-platform:2.6' into json-schema-doc-enhancement
ES-Six Nov 3, 2021
7a43f5e
Update .gitignore
ES-Six Nov 3, 2021
4e52891
Update json-schema.md
ES-Six Nov 3, 2021
56e9e96
Update json-schema.md
ES-Six Nov 3, 2021
7671ba8
Apply suggestions from code review
alanpoulain Nov 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/website
.idea/
58 changes: 58 additions & 0 deletions core/json-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource]
class Greeting
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: "integer")]
private ?int $id = null;

// [...]

public function getId(): ?int
{
return $this->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).
Expand Down