Skip to content

Commit

Permalink
cleanup fix for Unresolved reference while instantiating Encoding
Browse files Browse the repository at this point in the history
PR #68
  • Loading branch information
cebe committed Apr 7, 2020
1 parent 9572321 commit ff90fca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
8 changes: 4 additions & 4 deletions src/spec/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public function __construct(array $data)
if ($encodingData instanceof Encoding) {
$encoding[$property] = $encodingData;
} elseif (is_array($encodingData)) {
$schema = $this->schema->properties[$property] ?? null;
// Don't pass the schema if it's still an unresolved reference.
if (this->schema->properties[$property] instanceof Reference) {
if ($schema instanceof Reference) {
$encoding[$property] = new Encoding($encodingData);
}
else {
$encoding[$property] = new Encoding($encodingData, $this->schema->properties[$property] ?? null);
} else {
$encoding[$property] = new Encoding($encodingData, $schema);
}
} else {
$givenType = gettype($encodingData);
Expand Down
52 changes: 52 additions & 0 deletions tests/spec/MediaTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use cebe\openapi\Reader;
use cebe\openapi\spec\MediaType;
use cebe\openapi\spec\Example;
use cebe\openapi\spec\OpenApi;
use cebe\openapi\spec\Reference;
use Symfony\Component\Yaml\Yaml;

/**
* @covers \cebe\openapi\spec\MediaType
Expand Down Expand Up @@ -114,4 +116,54 @@ public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException

new MediaType($config);
}

public function testUnresolvedReferencesInEncoding()
{
$yaml = Yaml::parse(<<<'YAML'
openapi: "3.0.0"
info:
version: 1.0.0
title: Encoding test
paths:
/pets:
post:
summary: Create a pet
operationId: createPets
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
pet:
$ref: '#/components/schemas/Pet'
petImage:
type: string
format: binary
encoding:
pet:
contentType: application/json
petImage:
contentType: image/*
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'201':
description: Null response
components:
schemas:
Pet:
type: object
properties:
name:
type: string
YAML
);
$openapi = new OpenApi($yaml);
$result = $openapi->validate();

$this->assertEquals([], $openapi->getErrors());
$this->assertTrue($result);
}
}
50 changes: 0 additions & 50 deletions tests/spec/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,54 +239,4 @@ public function testSpecs($openApiFile)
}

}

public function testUnresolvedReferencesInEncoding($config, $expectedException)
{
$yaml = Yaml::parse(<<<'YAML'
openapi: "3.0.0"
info:
version: 1.0.0
title: Encoding test
paths:
/pets:
post:
summary: Create a pet
operationId: createPets
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
pet:
$ref: '#/components/schemas/Pet'
petImage:
type: string
format: binary
encoding:
pet:
contentType: application/json
petImage:
contentType: image/*
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'201':
description: Null response
components:
schemas:
Pet:
type: object
properties:
name:
type: string
YAML
);
$openapi = new OpenApi($yaml);
$result = $openapi->validate();

$this->assertEquals([], $openapi->getErrors());
$this->assertTrue($result);
}
}

0 comments on commit ff90fca

Please sign in to comment.