Skip to content

Commit 7870bce

Browse files
Ocramiusteohhanhui
authored andcommitted
Rolling back to mapping unknown object types as JSON-Schema {"type": "string"}
To avoid BC breaks, we defer this fix to #3403 > **API Platform version(s) affected**: 2.5.x > > **Description** > > In our tests for `TypeFactory`: > > ``` > > yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT)]; > yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true)]; > > // ... > > yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]; > yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)]; > ``` > > While reviewing #3402, @dunglas found a potential BC break with objects that may be used in URIs as `string`s (therefore not `objects`): > > * [#3402 (comment)](#3402 (comment)) > > * [#3402 (comment)](#3402 (comment)) > > > **How to reproduce** > > The test should instead convert `object` to `object`: > > ``` > > yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT)]; > yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, true)]; > > // ... > > yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]; > yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)]; > ```
1 parent 5a079e5 commit 7870bce

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/JsonSchema/TypeFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function makeBasicType(Type $type, string $format = 'json', ?bool $reada
117117
private function getClassType(?string $className, string $format, ?bool $readableLink, ?array $serializerContext, ?Schema $schema): array
118118
{
119119
if (null === $className) {
120-
return ['type' => 'object'];
120+
return ['type' => 'string'];
121121
}
122122

123123
if (is_a($className, \DateTimeInterface::class, true)) {
@@ -141,7 +141,7 @@ private function getClassType(?string $className, string $format, ?bool $readabl
141141

142142
// Skip if $schema is null (filters only support basic types)
143143
if (null === $schema) {
144-
return ['type' => 'object'];
144+
return ['type' => 'string'];
145145
}
146146

147147
if ($this->isResourceClass($className) && true !== $readableLink) {

tests/JsonSchema/TypeFactoryTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public function typeProvider(): iterable
4242
yield [['nullable' => true, 'type' => 'boolean'], new Type(Type::BUILTIN_TYPE_BOOL, true)];
4343
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_STRING)];
4444
yield [['nullable' => true, 'type' => 'string'], new Type(Type::BUILTIN_TYPE_STRING, true)];
45-
yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT)];
46-
yield [['nullable' => true, 'type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, true)];
45+
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT)];
46+
yield [['nullable' => true, 'type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true)];
4747
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)];
4848
yield [['nullable' => true, 'type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTimeImmutable::class)];
4949
yield [['type' => 'string', 'format' => 'duration'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateInterval::class)];
50-
yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
51-
yield [['nullable' => true, 'type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
50+
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
51+
yield [['nullable' => true, 'type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
5252
yield [['type' => 'array', 'items' => ['type' => 'string']], new Type(Type::BUILTIN_TYPE_STRING, false, null, true)];
5353
yield 'array can be itself nullable' => [
5454
['nullable' => true, 'type' => 'array', 'items' => ['type' => 'string']],
@@ -160,13 +160,13 @@ public function openAPIV2typeProvider(): iterable
160160
yield [['type' => 'boolean'], new Type(Type::BUILTIN_TYPE_BOOL, true)];
161161
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_STRING)];
162162
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_STRING, true)];
163-
yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT)];
164-
yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, true)];
163+
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT)];
164+
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true)];
165165
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)];
166166
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTimeImmutable::class)];
167167
yield [['type' => 'string', 'format' => 'duration'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateInterval::class)];
168-
yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
169-
yield [['type' => 'object'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
168+
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
169+
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
170170
yield [['type' => 'array', 'items' => ['type' => 'string']], new Type(Type::BUILTIN_TYPE_STRING, false, null, true)];
171171
yield 'array can be itself nullable, but ignored in OpenAPI V2' => [
172172
['type' => 'array', 'items' => ['type' => 'string']],

0 commit comments

Comments
 (0)