Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ private function addSchemas(Configuration $configuration, OpenApi $openApi): arr
$models = [];

foreach ($schemas as $name => $schema) {
if ($schema instanceof Reference) {
$schema = $schema->resolve();
}

if ($schema instanceof Schema) {
$classModel = $this->classTransformer->transform(
$configuration,
Expand Down
6 changes: 6 additions & 0 deletions src/Generator/ClassTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Reinfi\OpenApiModels\Model\ArrayType;
use Reinfi\OpenApiModels\Model\ClassModel;
use Reinfi\OpenApiModels\Model\Imports;
use Reinfi\OpenApiModels\Model\InlineSchemaReference;
use Reinfi\OpenApiModels\Model\OneOfReference;
use Reinfi\OpenApiModels\Model\OneOfType;
use Reinfi\OpenApiModels\Model\ScalarType;
Expand Down Expand Up @@ -504,6 +505,11 @@ private function resolveArrayType(
$arrayType = Types::OneOf;
}

if ($arrayType instanceof InlineSchemaReference) {
$itemsSchema = $arrayType->schema;
$arrayType = Types::Object;
}

if ($arrayType === Types::Object) {
$inlineObject = $this->transformInlineObject(
$configuration,
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/PropertyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Nette\PhpGenerator\PromotedParameter;
use openapiphp\openapi\spec\Reference;
use openapiphp\openapi\spec\Schema;
use Reinfi\OpenApiModels\Model\InlineSchemaReference;
use Reinfi\OpenApiModels\Model\OneOfReference;
use Reinfi\OpenApiModels\Model\ScalarType;

Expand All @@ -18,7 +19,7 @@ public function resolve(
string $name,
Schema|Reference $schema,
bool $required,
ScalarType|ClassReference|OneOfReference|Types|string $type,
ScalarType|ClassReference|InlineSchemaReference|OneOfReference|Types|string $type,
): PromotedParameter {
$property = $constructor->addPromotedParameter($name);

Expand Down
16 changes: 15 additions & 1 deletion src/Generator/ReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use openapiphp\openapi\spec\Schema;
use Reinfi\OpenApiModels\Exception\InvalidReferenceException;
use Reinfi\OpenApiModels\Model\SchemaWithName;
use Throwable;

readonly class ReferenceResolver
{
Expand All @@ -20,8 +21,21 @@ public function resolve(OpenApi $openApi, Reference $reference): SchemaWithName
$reference->getReference(),
$matches
) !== 1) {
try {
$resolvedSchema = $reference->resolve();

if ($resolvedSchema instanceof Schema) {
return new SchemaWithName(OpenApiType::Schemas, '', $resolvedSchema);
}
} catch (Throwable $throwable) {
throw new InvalidArgumentException(
sprintf('Invalid reference "%s" given, does not match pattern', $reference->getReference()),
previous: $throwable,
);
}

throw new InvalidArgumentException(
sprintf('Invalid reference "%s" given, does not match pattern', $reference->getReference())
sprintf('Invalid reference "%s" given, does not match pattern', $reference->getReference()),
);
}

Expand Down
9 changes: 7 additions & 2 deletions src/Generator/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use openapiphp\openapi\spec\OpenApi;
use openapiphp\openapi\spec\Reference;
use openapiphp\openapi\spec\Schema;
use Reinfi\OpenApiModels\Model\InlineSchemaReference;
use Reinfi\OpenApiModels\Model\OneOfReference;
use Reinfi\OpenApiModels\Model\ScalarType;

Expand All @@ -22,13 +23,13 @@ public function __construct(

/**
* @phpstan-assert Schema $schema when return type is string|Types|null
* @return ($schema is Reference ? ClassReference|OneOfReference|ScalarType : ($throwException is true ? string|Types : string|Types|null))
* @return ($schema is Reference ? ClassReference|InlineSchemaReference|OneOfReference|ScalarType : ($throwException is true ? string|Types : string|Types|null))
*/
public function resolve(
OpenApi $openApi,
Schema|Reference $schema,
bool $throwException = true
): ScalarType|ClassReference|OneOfReference|string|Types|null {
): ScalarType|ClassReference|InlineSchemaReference|OneOfReference|string|Types|null {
if ($schema instanceof Reference) {
$schemaWithName = $this->referenceResolver->resolve($openApi, $schema);

Expand All @@ -46,6 +47,10 @@ public function resolve(
return new ScalarType($referenceType, $schemaWithName->schema);
}

if ($schemaWithName->name === '') {
return new InlineSchemaReference($schemaWithName->schema);
}

return new ClassReference(
$schemaWithName->openApiType,
$this->namespaceResolver->resolveNamespace($schemaWithName->openApiType, $schemaWithName->schema)
Expand Down
15 changes: 15 additions & 0 deletions src/Model/InlineSchemaReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Reinfi\OpenApiModels\Model;

use openapiphp\openapi\spec\Schema;

readonly class InlineSchemaReference
{
public function __construct(
public Schema $schema
) {
}
}
39 changes: 36 additions & 3 deletions test/Generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public function testItGeneratesClassesFromOpenApi(): void
'Test2' => [
'type' => 'object',
],
'Test3' => [
'$ref' => '#/components/schemas/Test4',
],
],
],
]);
Expand Down Expand Up @@ -77,6 +74,12 @@ public function testItGeneratesRequestBodies(): void
$configuration = new Configuration([], '', '');

$openApi = new OpenApi([
'openapi' => '3.0.0',
'info' => [
'title' => 'Test API',
'version' => '1.0.0',
],
'paths' => [],
'components' => [
'requestBodies' => [
'Test1' => [
Expand Down Expand Up @@ -123,6 +126,12 @@ public function testItGeneratesResponses(): void
$configuration = new Configuration([], '', '');

$openApi = new OpenApi([
'openapi' => '3.0.0',
'info' => [
'title' => 'Test API',
'version' => '1.0.0',
],
'paths' => [],
'components' => [
'responses' => [
'Test1' => [
Expand Down Expand Up @@ -169,6 +178,12 @@ public function testItGeneratesReferenceClasses(): void
$configuration = new Configuration([], '', '');

$openApi = new OpenApi([
'openapi' => '3.0.0',
'info' => [
'title' => 'Test API',
'version' => '1.0.0',
],
'paths' => [],
'components' => [
'responses' => [
'Test1' => [
Expand Down Expand Up @@ -215,6 +230,12 @@ public function testItGeneratesOnlyJsonSchema(): void
$configuration = new Configuration([], '', '');

$openApi = new OpenApi([
'openapi' => '3.0.0',
'info' => [
'title' => 'Test API',
'version' => '1.0.0',
],
'paths' => [],
'components' => [
'responses' => [
'Test1' => [
Expand Down Expand Up @@ -270,6 +291,12 @@ public function testItThrowsExceptionIfJsonContentTypeNotFound(): void
$configuration = new Configuration([], '', '');

$openApi = new OpenApi([
'openapi' => '3.0.0',
'info' => [
'title' => 'Test API',
'version' => '1.0.0',
],
'paths' => [],
'components' => [
'responses' => [
'Test1' => [
Expand Down Expand Up @@ -304,6 +331,12 @@ public function testItSetsCommentIfTopLevelHasDescription(): void
$configuration = new Configuration([], '', '');

$openApi = new OpenApi([
'openapi' => '3.0.0',
'info' => [
'title' => 'Test API',
'version' => '1.0.0',
],
'paths' => [],
'components' => [
'responses' => [
'Test1' => [
Expand Down
14 changes: 11 additions & 3 deletions test/Generator/ReferenceResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ public function testItThrowsExceptionIfNotValidReference(): void
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage('Invalid reference "no-valid-reference" given, does not match pattern');

$openApi = new OpenApi([]);
$reference = new Reference([
'$ref' => 'no-valid-reference',
$openApi = new OpenApi([
'components' => [
'schemas' => [
'InvalidReference' => [
'$ref' => 'no-valid-reference',
],
],
],
]);

$reference = $openApi->components?->schemas['InvalidReference'];
self::assertInstanceOf(Reference::class, $reference);

$resolver = new ReferenceResolver();
$resolver->resolve($openApi, $reference);
}
Expand Down
Loading