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

docs: Enhance Json schema documentation #1455

merged 34 commits into from
Nov 4, 2021

Conversation

ES-Six
Copy link
Contributor

@ES-Six ES-Six commented Oct 28, 2021

This PR is intended to improve the JSON Schema documentation to specify the Overriding JSON Schema specification use case (usefull when making unit tests).

This PR concern an already existing functionnality that was undocumented.

@ES-Six ES-Six changed the title Enhancement Json schema documentation docs: Enhancement Json schema documentation Oct 28, 2021
@ES-Six ES-Six changed the title docs: Enhancement Json schema documentation docs: Enhance Json schema documentation Oct 28, 2021
Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition.

ES-Six and others added 9 commits October 29, 2021 08:23
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
@ES-Six ES-Six requested a review from dunglas October 29, 2021 06:44
@ES-Six
Copy link
Contributor Author

ES-Six commented Oct 30, 2021

All requested changes have been taken into account.

@ES-Six
Copy link
Contributor Author

ES-Six commented Nov 2, 2021

Do not hesitate if you need additional changes or improvements 😉.

@ES-Six
Copy link
Contributor Author

ES-Six commented Nov 3, 2021

Requested changes are now applied.

}
```

Be careful: the `json_schema_context` property does not always have the same properties as the `openapi_context` property.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? OpenAPI and JSON Schema should be fully compatible now.

Copy link
Contributor Author

@ES-Six ES-Six Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it while doing unit tests and using the assertMatchesResourceCollectionJsonSchema method and while testing on an entity with JSON column in it.

For this doctrine JSON column, I was using the openapi_context "type" => "json" to make it appear well in the documentation. But in a unit testing context, the JSON Schema automatically generated ignore the openapi_context.

Example of api property used in my project :

#[ApiProperty(attributes: [
        "openapi_context" => [
            "type" => "json",
            "example"=>"{...json content here...}"
        ]
    ])]

So I tried to set the JSON schema to "type" => "json".
Like this :
Example of api property used in my project :

#[ApiProperty(attributes: [
        "openapi_context" => [
            "type" => "json",
            "example"=>"{...json content here...}"
        ],
        "json_schema_context" => [
            "type" => "json"
        ]
    ])]

But it doesn't work because the type JSON is not a part of the JSON Schema specification, so unit test fails as it take the default type for this context inside API Platform instead.

Available JSON Schema types are : http://json-schema.org/understanding-json-schema/reference/type.html

So to solve the problem in this specific case, I need to use the type to make the unit test to pass

#[ApiProperty(attributes: [
        "openapi_context" => [
            "type" => "json",
            "example"=>"{...json content here...}"
        ],
        "json_schema_context" => [
            "type" => "array"
        ]
    ])]

It also seems to display well the example in the swagger doc, when I specify "type" => "array" for both, but the generated openapi schema is not correct on the swagger doc :

image

With "type" => "json" in the openapi_context the schema in the swagger do appear like this (excpected behaviour) :
image

With "type" => "array" in the openapi_context the schema in the swagger do appear like this (not the excpected behaviour) :

image

So the JSON Schema and OpenAPI seems not to have the exact same types in APIPlatform, so my initial intention is also to document this exeption in order to allow users to handle JSON column in their entities.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think json type is a valid data type. Swagger UI simply displays it by default as a string.

Copy link
Contributor Author

@ES-Six ES-Six Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? OpenAPI and JSON Schema should be fully compatible now.

I don't think json type is a valid data type. Swagger UI simply displays it by default as a string.

As I investigated more here is what I understood :

Yes, OpenAPI and JSON Schema are fully compatible. So I need to change the doc. Indeed I miss understood these 2 specifications.

The type property is only an informative text OR if it's a known value (example : string, integer, array, object, etc...) it will be used to handle specific things on the swagger doc.

Example :

#[ApiProperty(attributes: [
    "openapi_context" => [
        "type" => "json" // This is an informative text as it's an invalid data type
        // As no items is specified, swagger use a default "items" => ["type" => "string"] and rely on it as fallback
    ]
])]

So if "type" is an informative text by default, we can put anything we want in this property, so it is an unknown data type but it's a valid property.

So I only need to delete this problematic part because it's erroneous.

Copy link
Contributor Author

@ES-Six ES-Six Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This erroneous part is now removed, sorry for this error, I had trouble understanding what is exactly the purpose of the "type" property :)

@ES-Six
Copy link
Contributor Author

ES-Six commented Nov 3, 2021

Requested changes are now applied.

@alanpoulain alanpoulain merged commit f2d3767 into api-platform:2.6 Nov 4, 2021
@alanpoulain
Copy link
Member

Thank you @ES-Six.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants