Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 9, 2024
1 parent 4b8fb01 commit cafd5e0
Show file tree
Hide file tree
Showing 5 changed files with 3,289 additions and 2,143 deletions.
43 changes: 17 additions & 26 deletions src/Parser/OpenAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use PSX\Schema\DefinitionsInterface;
use PSX\Schema\Exception\InvalidSchemaException;
use PSX\Schema\Exception\TypeNotFoundException;
use PSX\Schema\Exception\UnknownTypeException;
use PSX\Schema\Inspector\Hash;
use PSX\Schema\Parser as SchemaParser;
use PSX\Schema\Parser\ContextInterface;
Expand All @@ -62,10 +63,12 @@
use PSX\Schema\SchemaTraverser;
use PSX\Schema\Type\ArrayType;
use PSX\Schema\Type\ArrayTypeInterface;
use PSX\Schema\Type\DefinitionTypeAbstract;
use PSX\Schema\Type\Factory\PropertyTypeFactory;
use PSX\Schema\Type\IntersectionType;
use PSX\Schema\Type\MapType;
use PSX\Schema\Type\MapTypeInterface;
use PSX\Schema\Type\PropertyTypeAbstract;
use PSX\Schema\Type\ReferencePropertyType;
use PSX\Schema\Type\ReferenceType;
use PSX\Schema\Type\StructDefinitionType;
Expand Down Expand Up @@ -376,8 +379,9 @@ private function parseResponseInRange(OpenAPIOperation $operation, int $start, i

/**
* @throws InvalidSchemaException
* @throws UnknownTypeException
*/
private function getSchemaFromMediaTypes(MediaTypes $mediaTypes): ?TypeInterface
private function getSchemaFromMediaTypes(MediaTypes $mediaTypes): ?PropertyTypeAbstract
{
$mediaType = $mediaTypes['application/json'] ?? null;
if (!$mediaType instanceof MediaType) {
Expand All @@ -389,9 +393,12 @@ private function getSchemaFromMediaTypes(MediaTypes $mediaTypes): ?TypeInterface
return null;
}

$type = $this->schemaParser->parsePropertyType($schema);

return $this->transformInlineStruct($type);
try {
return $this->schemaParser->parsePropertyType($schema);
} catch (UnknownTypeException $e) {
$type = $this->schemaParser->parseDefinitionType($schema);
return $this->transformInlineStruct($type);
}
}

/**
Expand All @@ -400,30 +407,14 @@ private function getSchemaFromMediaTypes(MediaTypes $mediaTypes): ?TypeInterface
*
* @throws InvalidSchemaException
*/
private function transformInlineStruct(TypeInterface $type): TypeInterface
private function transformInlineStruct(DefinitionTypeAbstract $type): PropertyTypeAbstract
{
if ($type instanceof StructDefinitionType) {
// we have an inline struct type we automatically add this ot the definitions, since we have no name we generate
// it based on the type, this should motivate users to move the definition to the components section
$typeName = 'Inline' . substr($this->hashInspector->generateByType($type), 0, 8);
$this->definitions->addType($typeName, $type);

return TypeFactory::getReference($typeName);
} elseif ($type instanceof MapTypeInterface) {
$schema = $type->getSchema();
if ($schema instanceof TypeInterface) {
$r = $this->transformInlineStruct($schema);
return TypeFactory::getMap($r);
}
} elseif ($type instanceof ArrayTypeInterface) {
$schema = $type->getSchema();
if ($schema instanceof TypeInterface) {
$r = $this->transformInlineStruct($schema);
return TypeFactory::getArray($r);
}
}
// we have an inline struct type we automatically add this ot the definitions, since we have no name we generate
// it based on the type, this should motivate users to move the definition to the components section
$typeName = 'Inline' . substr($this->hashInspector->generateByType($type), 0, 8);
$this->definitions->addType($typeName, $type);

return $type;
return PropertyTypeFactory::getReference($typeName);
}

private function resolveReference(string $reference)
Expand Down
2 changes: 1 addition & 1 deletion src/Transformer/OpenAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function transformOperation(\stdClass $operation, \stdClass $spec): void
if ($schema instanceof \stdClass) {
$result = $this->transformer->transform($schema);

$response->content->{'application/json'}->schema = ['$ref' => $result->{'$ref'}];
$response->content->{'application/json'}->schema = ['$ref' => $result->{'root'}];

foreach ($result->definitions as $name => $type) {
$spec->components->schemas->{$name} = $type;
Expand Down
2 changes: 1 addition & 1 deletion tests/Inspector/DevLifterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testGenerate()
$lifter = new DevLifter();

$actual = $lifter->elevate('0.1.0', $left, $right);
$expect = '0.2.0';
$expect = '0.1.1';

$this->assertEquals($expect, $actual);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Parser/OpenAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ public function testParseInline()
$this->assertTrue($operation->getArguments()->isEmpty());

$this->assertEquals(200, $operation->getReturn()->getCode());
$this->assertEquals(['$ref' => 'Inline01fd4b61'], $operation->getReturn()->getSchema()->toArray());
$this->assertEquals(['type' => 'reference', 'target' => 'Inline01fd4b61'], $operation->getReturn()->getSchema()->toArray());

$this->assertCount(0, $operation->getThrows());

$this->assertEquals([
'type' => 'object',
'type' => 'struct',
'properties' => [
'success' => TypeFactory::getBoolean(),
'message' => TypeFactory::getString(),
'success' => PropertyTypeFactory::getBoolean(),
'message' => PropertyTypeFactory::getString(),
],
], $definitions->getType('Inline01fd4b61')->toArray());
}
Expand Down
Loading

0 comments on commit cafd5e0

Please sign in to comment.