Skip to content

Commit 9572321

Browse files
authored
Merge pull request #68 from karsa-mistmere/patch-1
Unresolved reference while instantiating Encoding
2 parents 98fedaf + 98d0465 commit 9572321

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/spec/MediaType.php

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

tests/spec/OpenApiTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,54 @@ 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+
}
242292
}

0 commit comments

Comments
 (0)