Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
// Strictly speaking, it is possible to include `@id` or `@context` in the input,
// but the generated JSON Schema does not include `"additionalProperties": false` by default,
// so it is possible to include `@id` or `@context` in the input even if the input schema is a JSON schema.
if (Schema::TYPE_INPUT === $type) {
if (Schema::TYPE_INPUT === $type && 'jsonapi' !== $format) {
$format = 'json';
}

Expand Down
8 changes: 7 additions & 1 deletion src/JsonApi/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin

$schema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, $schema, $jsonApiSerializerContext, $forceCollection);
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $jsonApiSerializerContext + ['schema_type' => $type]);
if (Schema::TYPE_INPUT === $type) {
$definitionName .= '.input';
}
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
$definitions = $schema->getDefinitions();
$collectionKey = $schema->getItemsDefinitionKey();
Expand Down Expand Up @@ -295,7 +298,7 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
if ($relation = $this->getRelationship($className, $propertyName, $serializerContext)) {
[$isOne, $relatedClasses] = $relation;
$refs = [];
foreach ($relatedClasses as $relatedClassName => $hasOperations) {
foreach (Schema::TYPE_OUTPUT === $type ? $relatedClasses : [] as $relatedClassName => $hasOperations) {
if (false === $hasOperations) {
continue;
}
Expand Down Expand Up @@ -358,6 +361,9 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
'type' => 'object',
'properties' => $relationships,
];
}

if (Schema::TYPE_OUTPUT === $type && \count($relationships) > 0) {
$included = [
'included' => [
'description' => 'Related resources requested via the "include" query parameter.',
Expand Down
38 changes: 38 additions & 0 deletions tests/Functional/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,44 @@ public function testOpenApiReferenceInResponsesUsesDollarRef(): void
$this->assertArrayNotHasKey('ref', $responses['401']);
}

public function testNonJsonLdRequestBodiesKeepTheirFormatDefinition(): void
{
$json = self::createClient()->request('GET', '/docs', [
'headers' => ['Accept' => 'application/vnd.openapi+json'],
])->toArray();

$content = $json['paths']['/dummies']['post']['requestBody']['content'];

$this->assertSame('#/components/schemas/Dummy', $content['application/xml']['schema']['$ref']);
$this->assertSame('#/components/schemas/Dummy', $content['application/ld+json']['schema']['$ref']);
}

public function testJsonApiRequestBodyUsesItsOwnInputDefinition(): void
{
$json = self::createClient()->request('GET', '/docs', [
'headers' => ['Accept' => 'application/vnd.openapi+json'],
])->toArray();
$schemas = $json['components']['schemas'];

$inputName = $this->definitionName($json['paths']['/dummies']['post']['requestBody']['content']['application/vnd.api+json']['schema']['$ref']);
$outputName = $this->definitionName($json['paths']['/dummies/{id}']['get']['responses']['200']['content']['application/vnd.api+json']['schema']['$ref']);

$this->assertSame('Dummy.jsonapi', $outputName);
$this->assertNotSame($outputName, $inputName);

$input = $schemas[$inputName];
$this->assertSame(['type'], $input['properties']['data']['required']);
$this->assertArrayHasKey('attributes', $input['properties']['data']['properties']);
$this->assertArrayNotHasKey('included', $input['properties']);

$this->assertSame(['type', 'id'], $schemas[$outputName]['properties']['data']['required']);
}

private function definitionName(string $ref): string
{
return substr($ref, \strlen('#/components/schemas/'));
}

public function testDeprecatedPutDoesNotLeakIntoNestedResourceSchema(): void
{
$response = self::createClient()->request('GET', '/docs', [
Expand Down
Loading