Skip to content
Draft
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"require": {
"php": "^8.0",
"yoanm/jsonrpc-server-doc-sdk": "^1.0"
"yoanm/jsonrpc-server-doc-sdk": "^1.0.1"
},
"require-dev": {
"ext-json": "*",
Expand Down
16 changes: 4 additions & 12 deletions features/method-doc.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ Feature: MethodDocNormalizer
"array-val": {
"type": "array",
"x-nullable": true,
"items": {
"type": "string"
}
"items": {}
}
}
}
Expand Down Expand Up @@ -111,9 +109,7 @@ Feature: MethodDocNormalizer
"array-val": {
"type": "array",
"x-nullable": true,
"items": {
"type": "string"
}
"items": {}
}
}
}
Expand Down Expand Up @@ -384,9 +380,7 @@ Feature: MethodDocNormalizer
"array-val": {
"type": "array",
"x-nullable": true,
"items": {
"type": "string"
}
"items": {}
}
}
},
Expand All @@ -401,9 +395,7 @@ Feature: MethodDocNormalizer
"array-val": {
"type": "array",
"x-nullable": true,
"items": {
"type": "string"
}
"items": {}
}
}
},
Expand Down
10 changes: 2 additions & 8 deletions features/type-doc.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Feature: TypeDoc normalization
Then I should have the following TypeDoc:
"""
{
"type": "string",
"x-nullable": true
}
"""
Expand All @@ -19,7 +18,6 @@ Feature: TypeDoc normalization
Then I should have the following TypeDoc:
"""
{
"type": "string",
"x-nullable": true
}
"""
Expand Down Expand Up @@ -93,9 +91,7 @@ Feature: TypeDoc normalization
{
"type": "array",
"x-nullable": true,
"items": {
"type": "string"
}
"items": {}
}
"""

Expand All @@ -108,9 +104,7 @@ Feature: TypeDoc normalization
{
"type": "array",
"x-nullable": true,
"items": {
"type": "string"
}
"items": {}
}
"""

Expand Down
11 changes: 2 additions & 9 deletions features/type-doc_fully-configured.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Feature: TypeDoc normalization
"""
{
"description": "type-b-description",
"type": "string",
"x-nullable": false,
"default": "type-b-default",
"example": "type-b-example",
Expand Down Expand Up @@ -48,7 +47,6 @@ Feature: TypeDoc normalization
"""
{
"description": "type-b-description",
"type": "string",
"x-nullable": false,
"default": "type-b-default",
"example": "type-b-example",
Expand Down Expand Up @@ -265,9 +263,7 @@ Feature: TypeDoc normalization
"enum": [["type-b-allowed-value-a"], ["type-b-allowed-value-b"]],
"minItems": 2,
"maxItems": 8,
"items": {
"type": "string"
}
"items": {}
}
"""

Expand Down Expand Up @@ -304,10 +300,7 @@ Feature: TypeDoc normalization
"enum": [["type-b-allowed-value-a"], ["type-b-allowed-value-b"]],
"minItems": 2,
"maxItems": 8,
"items": {
"type": "string",
"x-nullable": true
}
"items": {"x-nullable": true}
}
"""

Expand Down
6 changes: 3 additions & 3 deletions src/App/Normalizer/Component/SchemaTypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class SchemaTypeNormalizer
/**
* @param TypeDoc $doc
*
* @return string
* @return ?string
*
* @throws \ReflectionException
*/
public function normalize(TypeDoc $doc) : string
public function normalize(TypeDoc $doc) : ?string
{
$type = str_replace('Doc', '', lcfirst((new \ReflectionClass($doc))->getShortName()));
if (in_array($type, self::MANAGED_TYPE_LIST)) {
Expand All @@ -46,6 +46,6 @@ public function normalize(TypeDoc $doc) : string
return self::RENAMED_TYPE_LIST[$type];
}

return 'string';
return null;
}
}
22 changes: 13 additions & 9 deletions src/App/Normalizer/Component/TypeDocNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function normalize(TypeDoc $doc) : array
$format = ($doc instanceof StringDoc ? $doc->getFormat() : null);

return $this->appendIfValueNotNull('description', $doc->getDescription())
+ ['type' => $this->schemaTypeNormalizer->normalize($doc)]
+ $this->appendIfValueNotNull('type', $this->schemaTypeNormalizer->normalize($doc))
+ $this->appendIfValueNotNull('format', $format)
+ ['x-nullable' => $doc->isNullable()]
+ $paramDocRequired
Expand Down Expand Up @@ -104,7 +104,8 @@ protected function appendArrayDoc(TypeDoc $doc, array $siblingsDoc) : array
if ($doc instanceof ArrayDoc && null !== $doc->getItemValidation()) {
$siblingsDoc['items'] = $this->normalize($doc->getItemValidation());
} else {
$siblingsDoc['items']['type'] = $this->guessItemsType($doc->getSiblingList());
$type = $this->guessItemsType($doc->getSiblingList());
$siblingsDoc['items'] = null !== $type ? ['type' => $type] : [];
}

return $siblingsDoc;
Expand Down Expand Up @@ -162,21 +163,24 @@ function (array $carry, TypeDoc $sibling) {
*
* @return string
*/
protected function guessItemsType(array $siblingList) : string
protected function guessItemsType(array $siblingList) : ?string
{
$self = $this;
$uniqueTypeList = array_unique(
array_map(
function (TypeDoc $sibling) use ($self) {
return $self->schemaTypeNormalizer->normalize($sibling);
},
$siblingList
array_filter(
array_map(
function (TypeDoc $sibling) use ($self) {
return $self->schemaTypeNormalizer->normalize($sibling);
},
$siblingList
),
static fn ($value) => (null !== $value)
)
);

if (count($uniqueTypeList) !== 1) {
// default string if sub item type not guessable
return 'string';
return null;
}

return array_shift($uniqueTypeList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function testShouldHandle($typeDoc, $expected)
);
}

public function testShouldFallbackToString()
public function testShouldFallbackToUnknownType()
{
$this->assertSame(
'string',
null,
$this->normalizer->normalize(new NotManagedTypeDoc())
);
}
Expand All @@ -53,6 +53,10 @@ public function testShouldFallbackToString()
public function provideManagedTypeDocClassList()
{
return [
'unknown type' => [
'typeDocClass' => new TypeDocNS\TypeDoc(),
'expected' => null,
],
'array type' => [
'typeDocClass' => new TypeDocNS\ArrayDoc(),
'expected' => 'array'
Expand Down Expand Up @@ -83,7 +87,7 @@ public function provideManagedTypeDocClassList()
],
'scalar type' => [
'typeDocClass' => new TypeDocNS\ScalarDoc(),
'expected' => 'string'
'expected' => null, // Swagger 2.0 doesn't allow multiple type !
],
'string type' => [
'typeDocClass' => new TypeDocNS\StringDoc(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ public function provideSimpleManagedTypeDocList()
'Simple Doc' => [
'typeDoc' => new TypeDocNs\TypeDoc(),
'expected' => [
'type' => 'string',
'x-nullable' => true,
],
],
'Simple ScalarDoc' => [
'typeDoc' => new TypeDocNs\ScalarDoc(),
'expected' => [
'type' => 'string',
'x-nullable' => true,
],
],
Expand Down Expand Up @@ -99,15 +97,15 @@ public function provideSimpleManagedTypeDocList()
'expected' => [
'type' => 'array',
'x-nullable' => true,
'items' => ['type' => 'string']
'items' => []
],
],
'Simple ArrayDoc' => [
'typeDoc' => new TypeDocNs\ArrayDoc(),
'expected' => [
'type' => 'array',
'x-nullable' => true,
'items' => ['type' => 'string']
'items' => []
],
],
'Simple ObjectDoc' => [
Expand Down Expand Up @@ -138,7 +136,6 @@ public function provideFullyDefinedManagedTypeDocList()
->setDefault('my-default'),
'expected' => [
'description' => 'my-description',
'type' => 'string',
'x-nullable' => false,
'default' => 'my-default',
'example' => 'my-example',
Expand All @@ -155,7 +152,6 @@ public function provideFullyDefinedManagedTypeDocList()
->setDefault('my-default'),
'expected' => [
'description' => 'my-description',
'type' => 'string',
'x-nullable' => false,
'default' => 'my-default',
'example' => 'my-example',
Expand Down Expand Up @@ -290,7 +286,7 @@ public function provideFullyDefinedManagedTypeDocList()
'example' => ['my-example'],
'minItems' => 2,
'maxItems' => 5,
'items' => ['type' => 'string'],
'items' => [],
],
],
'Fully defined ArrayDoc' => [
Expand Down Expand Up @@ -363,7 +359,6 @@ public function provideBasicManagedTypeDocList()
->addAllowedValue(1)
->addAllowedValue(23),
'expected' => [
'type' => 'string',
'x-nullable' => true,
'enum' => [1, 23],
],
Expand All @@ -376,7 +371,6 @@ public function provideBasicManagedTypeDocList()
,
'expected' => [
'description' => 'my-description',
'type' => 'string',
'x-nullable' => false,
'example' => 'my-example',
],
Expand Down Expand Up @@ -474,7 +468,7 @@ public function provideBasicManagedTypeDocList()
'example' => ['my-example'],
'minItems' => 2,
'maxItems' => 5,
'items' => ['type' => 'string'],
'items' => [],
],
],
'Basic ObjectDoc' => [
Expand Down