Skip to content

Commit fc1f781

Browse files
authored
Fix error linking (#3)
1 parent f79e53b commit fc1f781

File tree

5 files changed

+169
-46
lines changed

5 files changed

+169
-46
lines changed

src/App/Normalizer/Component/OperationDocNormalizer.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,20 @@ public function __construct(
4141
*/
4242
public function normalize(MethodDoc $method, ServerDoc $serverDoc) : array
4343
{
44-
$self = $this
45-
;
44+
$self = $this;
4645

47-
$extraErrorDefinitionIdRefList = array_merge(
48-
array_map(
49-
function (ErrorDoc $errorDoc) use ($self) {
50-
return [
51-
'$ref' => $self->definitionRefResolver->getDefinitionRef(
52-
$self->definitionRefResolver->getErrorDefinitionId(
53-
$errorDoc,
54-
DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
55-
)
56-
)
57-
];
58-
},
59-
$serverDoc->getServerErrorList()
60-
),
61-
array_map(
62-
function (ErrorDoc $errorDoc) use ($self) {
63-
return [
64-
'$ref' => $self->definitionRefResolver->getDefinitionRef(
65-
$self->definitionRefResolver->getErrorDefinitionId(
66-
$errorDoc,
67-
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
68-
)
46+
$extraErrorDefinitionIdRefList = array_map(
47+
function (ErrorDoc $errorDoc) use ($self) {
48+
return [
49+
'$ref' => $self->definitionRefResolver->getDefinitionRef(
50+
$self->definitionRefResolver->getErrorDefinitionId(
51+
$errorDoc,
52+
DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
6953
)
70-
];
71-
},
72-
$serverDoc->getGlobalErrorList()
73-
)
54+
)
55+
];
56+
},
57+
$serverDoc->getServerErrorList()
7458
);
7559

7660
$docDescription = $docTags = [];

src/App/Normalizer/Component/ResponseDocNormalizer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,15 @@ protected function getMethodErrorArrayDoc(MethodDoc $method, array $extraErrorDe
9494
$errorDocList = array_merge(
9595
array_map(
9696
function ($errorRef) use ($self) {
97-
return ['$ref' => $self->definitionRefResolver->getDefinitionRef($errorRef)];
97+
$errorDoc = new ErrorDoc('', 0, null, null, $errorRef);
98+
return [
99+
'$ref' => $self->definitionRefResolver->getDefinitionRef(
100+
$self->definitionRefResolver->getErrorDefinitionId(
101+
$errorDoc,
102+
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
103+
)
104+
)
105+
];
98106
},
99107
$method->getGlobalErrorRefList()
100108
),

src/Infra/Normalizer/DocNormalizer.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,15 @@ protected function serverArray(HttpServerDoc $doc)
6868
$serverList = [];
6969
if (null !== $doc->getHost()) {
7070
$host = $doc->getHost();
71-
if (null !== $host) {
72-
if (null !== $doc->getBasePath()) {
73-
$host .= $doc->getBasePath();
74-
}
75-
$schemeList = $doc->getSchemeList();
76-
if (0 === count($schemeList)) {
77-
$schemeList[] = 'http';
78-
}
79-
foreach ($schemeList as $scheme) {
80-
$serverList[] = ['url' => sprintf('%s://%s', $scheme, $host)];
81-
}
71+
if (null !== $doc->getBasePath()) {
72+
$host .= $doc->getBasePath();
73+
}
74+
$schemeList = $doc->getSchemeList();
75+
if (0 === count($schemeList)) {
76+
$schemeList[] = 'http';
77+
}
78+
foreach ($schemeList as $scheme) {
79+
$serverList[] = ['url' => sprintf('%s://%s', $scheme, $host)];
8280
}
8381
}
8482

tests/Functional/App/Normalizer/Component/OperationDocNormalizerTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class OperationDocNormalizerTest extends TestCase
2929

3030
const DEFAULT_REQUEST_DEFINITION = ['default-request-definition'];
3131
const DEFAULT_RESPONSE_DEFINITION = ['default-response-definition'];
32+
const DEFAULT_RESPONSE_WITH_SERVER_ERRORS_DEFINITION = ['default-response-with-servers-errors-definition'];
3233

3334
public function setUp()
3435
{
@@ -54,9 +55,14 @@ public function testShouldHandle(MethodDoc $methodDoc, ServerDoc $serverDoc, $ex
5455
$this->requestDocTransformer->normalize($methodDoc)
5556
->willReturn(self::DEFAULT_REQUEST_DEFINITION)->shouldBeCalled()
5657
;
57-
$this->responseDocNormalizer->normalize($methodDoc, Argument::type('array'))
58-
->willReturn(self::DEFAULT_RESPONSE_DEFINITION)->shouldBeCalled()
59-
;
58+
if (count($serverDoc->getServerErrorList()) > 0) {
59+
$this->responseDocNormalizer->normalize($methodDoc, Argument::type('array'))
60+
->willReturn(self::DEFAULT_RESPONSE_WITH_SERVER_ERRORS_DEFINITION)->shouldBeCalled()
61+
;
62+
} else {
63+
$this->responseDocNormalizer->normalize($methodDoc, [])
64+
->willReturn(self::DEFAULT_RESPONSE_DEFINITION)->shouldBeCalled();
65+
}
6066

6167

6268

@@ -140,6 +146,30 @@ public function provideManagedMethodDocList()
140146
]
141147
],
142148
],
149+
'Server with custom errors' => [
150+
'methodDoc' => new MethodDoc('my-method-name', 'MethodId'),
151+
'serverDoc' => (new ServerDoc())
152+
->addServerError(new ErrorDoc('Custom1', 1))
153+
->addServerError(new ErrorDoc('Custom2', 2)),
154+
'expected' => [
155+
'summary' => '"my-method-name" json-rpc method',
156+
'operationId' => 'MethodId',
157+
'requestBody' => [
158+
'required' => true,
159+
'content' => [
160+
'application/json' => ['schema' => self::DEFAULT_REQUEST_DEFINITION]
161+
]
162+
],
163+
'responses' => [
164+
'200' => [
165+
'description' => 'JSON-RPC response',
166+
'content' => [
167+
'application/json' => ['schema' => self::DEFAULT_RESPONSE_WITH_SERVER_ERRORS_DEFINITION]
168+
]
169+
]
170+
]
171+
],
172+
],
143173
'Fully configured operation' => [
144174
'methodDoc' => (new MethodDoc('my-method-name', 'MethodId'))
145175
->addTag('tag1')

tests/Functional/App/Normalizer/Component/ResponseDocNormalizerTest.php

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component\ResponseDocNormalizer;
88
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component\ShapeNormalizer;
99
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver;
10+
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
1011
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
1112
use Yoanm\JsonRpcServerDoc\Domain\Model\Type as TypeDocNS;
1213

@@ -67,9 +68,6 @@ public function testShouldHandleBasicResponse()
6768
public function testShouldHandleResponseWithMethodResult()
6869
{
6970
$responseShape = ['responseShape'];
70-
$defaultErrorRef = 'default-error-ref';
71-
$methodResultDefinitionId = 'method-result-definition-id';
72-
$methodResultDefinitionIdRef = 'method-result-definition-id-ref';
7371

7472
/** @var TypeDocNS\TypeDoc|ObjectProphecy $methodResultDoc */
7573
$methodResultDoc = $this->prophesize(TypeDocNS\TypeDoc::class);
@@ -104,4 +102,109 @@ public function testShouldHandleResponseWithMethodResult()
104102
$this->normalizer->normalize($method)
105103
);
106104
}
105+
106+
public function testShouldHandleResponseWithCustomMethodError()
107+
{
108+
$responseShape = ['responseShape'];
109+
$errorId = 'Error-id';
110+
$errorId2 = 'Error-id-2';
111+
112+
/** @var ErrorDoc|ObjectProphecy $customMethodErrorDoc */
113+
$customMethodErrorDoc = $this->prophesize(ErrorDoc::class);
114+
/** @var ErrorDoc|ObjectProphecy $customMethodErrorDoc2 */
115+
$customMethodErrorDoc2 = $this->prophesize(ErrorDoc::class);
116+
/** @var MethodDoc $method */
117+
$method = (new MethodDoc('method-name'))
118+
->addCustomError($customMethodErrorDoc->reveal())
119+
->addCustomError($customMethodErrorDoc2->reveal())
120+
;
121+
122+
$customMethodErrorDoc->getIdentifier()
123+
->willReturn($errorId)
124+
->shouldBeCalled()
125+
;
126+
$customMethodErrorDoc2->getIdentifier()
127+
->willReturn($errorId2)
128+
->shouldBeCalled()
129+
;
130+
131+
$this->shapeNormalizer->getResponseShapeDefinition()
132+
->willReturn($responseShape)
133+
->shouldBeCalled()
134+
;
135+
136+
$this->assertSame(
137+
[
138+
'allOf' => [
139+
$responseShape,
140+
[
141+
'type' => 'object',
142+
'properties' => [
143+
'result' => [
144+
'description' => 'Method result'
145+
]
146+
],
147+
],
148+
[
149+
'type' => 'object',
150+
'properties' => [
151+
'error' => [
152+
'oneOf' => [
153+
['$ref' => '#/components/schemas/Error-Error-id'],
154+
['$ref' => '#/components/schemas/Error-Error-id-2'],
155+
]
156+
]
157+
],
158+
],
159+
],
160+
],
161+
$this->normalizer->normalize($method)
162+
);
163+
}
164+
165+
public function testShouldHandleResponseWithMethodWithGlobalErrorRef()
166+
{
167+
$responseShape = ['responseShape'];
168+
$errorId = 'Error-id';
169+
$errorId2 = 'Error-id-2';
170+
171+
/** @var MethodDoc $method */
172+
$method = (new MethodDoc('method-name'))
173+
->addGlobalErrorRef($errorId)
174+
->addGlobalErrorRef($errorId2)
175+
;
176+
177+
$this->shapeNormalizer->getResponseShapeDefinition()
178+
->willReturn($responseShape)
179+
->shouldBeCalled()
180+
;
181+
182+
$this->assertSame(
183+
[
184+
'allOf' => [
185+
$responseShape,
186+
[
187+
'type' => 'object',
188+
'properties' => [
189+
'result' => [
190+
'description' => 'Method result'
191+
]
192+
],
193+
],
194+
[
195+
'type' => 'object',
196+
'properties' => [
197+
'error' => [
198+
'oneOf' => [
199+
['$ref' => '#/components/schemas/Error-Error-id'],
200+
['$ref' => '#/components/schemas/Error-Error-id-2'],
201+
]
202+
]
203+
],
204+
],
205+
],
206+
],
207+
$this->normalizer->normalize($method)
208+
);
209+
}
107210
}

0 commit comments

Comments
 (0)