Skip to content

Commit ff90fca

Browse files
committed
cleanup fix for Unresolved reference while instantiating Encoding
PR #68
1 parent 9572321 commit ff90fca

File tree

3 files changed

+56
-54
lines changed

3 files changed

+56
-54
lines changed

src/spec/MediaType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function __construct(array $data)
5353
if ($encodingData instanceof Encoding) {
5454
$encoding[$property] = $encodingData;
5555
} elseif (is_array($encodingData)) {
56+
$schema = $this->schema->properties[$property] ?? null;
5657
// Don't pass the schema if it's still an unresolved reference.
57-
if (this->schema->properties[$property] instanceof Reference) {
58+
if ($schema instanceof Reference) {
5859
$encoding[$property] = new Encoding($encodingData);
59-
}
60-
else {
61-
$encoding[$property] = new Encoding($encodingData, $this->schema->properties[$property] ?? null);
60+
} else {
61+
$encoding[$property] = new Encoding($encodingData, $schema);
6262
}
6363
} else {
6464
$givenType = gettype($encodingData);

tests/spec/MediaTypeTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
use cebe\openapi\Reader;
44
use cebe\openapi\spec\MediaType;
55
use cebe\openapi\spec\Example;
6+
use cebe\openapi\spec\OpenApi;
67
use cebe\openapi\spec\Reference;
8+
use Symfony\Component\Yaml\Yaml;
79

810
/**
911
* @covers \cebe\openapi\spec\MediaType
@@ -114,4 +116,54 @@ public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException
114116

115117
new MediaType($config);
116118
}
119+
120+
public function testUnresolvedReferencesInEncoding()
121+
{
122+
$yaml = Yaml::parse(<<<'YAML'
123+
openapi: "3.0.0"
124+
info:
125+
version: 1.0.0
126+
title: Encoding test
127+
paths:
128+
/pets:
129+
post:
130+
summary: Create a pet
131+
operationId: createPets
132+
requestBody:
133+
content:
134+
multipart/form-data:
135+
schema:
136+
type: object
137+
properties:
138+
pet:
139+
$ref: '#/components/schemas/Pet'
140+
petImage:
141+
type: string
142+
format: binary
143+
encoding:
144+
pet:
145+
contentType: application/json
146+
petImage:
147+
contentType: image/*
148+
application/json:
149+
schema:
150+
$ref: '#/components/schemas/Pet'
151+
responses:
152+
'201':
153+
description: Null response
154+
components:
155+
schemas:
156+
Pet:
157+
type: object
158+
properties:
159+
name:
160+
type: string
161+
YAML
162+
);
163+
$openapi = new OpenApi($yaml);
164+
$result = $openapi->validate();
165+
166+
$this->assertEquals([], $openapi->getErrors());
167+
$this->assertTrue($result);
168+
}
117169
}

tests/spec/OpenApiTest.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -239,54 +239,4 @@ public function testSpecs($openApiFile)
239239
}
240240

241241
}
242-
243-
public function testUnresolvedReferencesInEncoding($config, $expectedException)
244-
{
245-
$yaml = Yaml::parse(<<<'YAML'
246-
openapi: "3.0.0"
247-
info:
248-
version: 1.0.0
249-
title: Encoding test
250-
paths:
251-
/pets:
252-
post:
253-
summary: Create a pet
254-
operationId: createPets
255-
requestBody:
256-
content:
257-
multipart/form-data:
258-
schema:
259-
type: object
260-
properties:
261-
pet:
262-
$ref: '#/components/schemas/Pet'
263-
petImage:
264-
type: string
265-
format: binary
266-
encoding:
267-
pet:
268-
contentType: application/json
269-
petImage:
270-
contentType: image/*
271-
application/json:
272-
schema:
273-
$ref: '#/components/schemas/Pet'
274-
responses:
275-
'201':
276-
description: Null response
277-
components:
278-
schemas:
279-
Pet:
280-
type: object
281-
properties:
282-
name:
283-
type: string
284-
YAML
285-
);
286-
$openapi = new OpenApi($yaml);
287-
$result = $openapi->validate();
288-
289-
$this->assertEquals([], $openapi->getErrors());
290-
$this->assertTrue($result);
291-
}
292242
}

0 commit comments

Comments
 (0)