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
2 changes: 1 addition & 1 deletion src/ReferenceResolver/LocalReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function resolve(Reference $reference): Document

foreach ($tryKeys as $tryKey) {
if ($this->rootDocument->has($tryKey)) {
/** @var array{"$id"?: string, id?: string, ...}|string $resolvedDocument */
/** @var array{"$id"?: string, id?: string, ...<mixed>}|string $resolvedDocument */
$resolvedDocument = $this->rootDocument->get($tryKey);
if (!is_array($resolvedDocument)) {
throw DocumentParseError::invalidContent($tryKey);
Expand Down
13 changes: 8 additions & 5 deletions src/Service/InlineService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function inline(string $schemaFile): Document

$this->components = [];
$this->allOfPaths = [];
$document = $this->processDocument($schemaDocument);
$document = $this->processDocument($schemaDocument, $schemaDocument);
$allOfPaths = array_unique($this->allOfPaths);
if ($allOfPaths) {
$merger = new AllOfMerger($document);
Expand Down Expand Up @@ -98,6 +98,7 @@ private function getRefName(Reference $reference, SchemaType $type): string

private function processDocument(
Document&WritableDocument $document,
Document $fileRoot,
?Reference $rootReference = null,
?string $parentPath = null,
): Document&WritableDocument {
Expand All @@ -107,7 +108,7 @@ private function processDocument(
$type = $this->resolveSchemaType($documentPaths);
$refName = $this->getRefName($reference, $type);

$nextParentPath = $parentPath;
$nextParentPath = ($parentPath ?? '') . $documentPaths[0];
if (
isset($this->components[$type->name][$refName])
&& $this->components[$type->name][$refName]
Expand All @@ -134,24 +135,26 @@ private function processDocument(
}

if ($reference->isInternal()) {
$subDocument = $document->get($reference->getPath());
$subDocument = $fileRoot->get($reference->getPath());
if (!is_array($subDocument)) {
throw new \BadMethodCallException('Failed to resolve internal model properly');
}
$referenceDocument = $this->documentFactory->createFromArray(
$refName,
$subDocument, // @phpstan-ignore argument.type
);
$nextFileRoot = $fileRoot;
}
else {
$referenceDocument = BasicDocument::fromDocument($this->referenceResolver->resolve($reference));
$nextParentPath .= $documentPaths[0];
$nextFileRoot = $referenceDocument;
}
$schemaId = $referenceDocument->get()['$id'] ?? $refName;
// reserve schema to avoid infinite recursion
$this->components[$type->name][$refName] = $schemaId;
$this->components[$type->name][$refName] = $this->processDocument(
$referenceDocument,
$nextFileRoot,
$reference,
$nextParentPath,
)->get();
Expand Down Expand Up @@ -198,7 +201,7 @@ private function processDocumentPaths(
elseif ($processAllOf && str_contains($path, '/allOf/')) {
$stripped = substr($path, 0, (int)strpos($path, '/allOf/'));
$this->modelAllOfPaths[$parentName][] = $stripped;
$this->allOfPaths[] = $parentPath . $stripped;
$this->allOfPaths[] = ($parentPath ?? '') . $stripped;
}
$document->set($path, $schema);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"openapi": "3.0.0",
"paths": {
"/test": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"sub": {
"properties": {
"subIntermediate": {
"allOf": [
{
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"$id": "Base"
},
{
"type": "object",
"properties": {
"intermediate": {
"type": "boolean"
}
}
}
]
}
},
"$id": "Intermediate"
}
},
"$id": "NestedAllOf"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Base": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
},
"Intermediate": {
"properties": {
"subIntermediate": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"intermediate": {
"type": "boolean"
}
}
}
}
},
"NestedAllOf": {
"type": "object",
"properties": {
"sub": {
"properties": {
"subIntermediate": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"intermediate": {
"type": "boolean"
}
}
}
},
"$id": "Intermediate"
}
}
}
}
}
}
54 changes: 54 additions & 0 deletions tests/inlined-schemas/inline-allOf-internal-nested/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"openapi": "3.0.0",
"paths": {
"/test": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NestedAllOf"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Base": {
"type": "object",
"properties": {
"id": { "type": "string" }
}
},
"Intermediate": {
"properties": {
"subIntermediate": {
"allOf": [
{ "$ref": "#/components/schemas/Base" },
{
"type": "object",
"properties": {
"intermediate": { "type": "boolean" }
}
}
]
}
}
},
"NestedAllOf": {
"type": "object",
"properties": {
"sub": {
"$ref": "#/components/schemas/Intermediate"
}
}
}
}
}
}
155 changes: 155 additions & 0 deletions tests/inlined-schemas/inline-nested-internal-refs/schema.dist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"openapi": "3.0.0",
"info": {
"title": "parking api",
"version": "1.0",
"contact": {
"name": "Stefna ehf",
"url": "https://stefna.is",
"email": "no-support@stefna.is"
}
},
"paths": {
"/customer": {
"post": {
"summary": "Create new customer",
"operationId": "createCustomer",
"responses": {
"201": {
"description": "OK"
}
},
"requestBody": {
"description": "Create customer payload",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"customer": {
"title": "Customer",
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"name": {
"type": "string",
"nullable": true,
"example": "Albert Albertsson"
},
"options": {
"type": "object",
"properties": {
"billingSsn": {
"type": "string"
}
},
"$id": "CustomerOption"
}
},
"$id": "Customer"
}
},
"required": [
"customer"
]
}
}
},
"$id": "CreateCustomerPayload"
}
}
}
},
"components": {
"schemas": {
"Customer": {
"title": "Customer",
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"name": {
"type": "string",
"nullable": true,
"example": "Albert Albertsson"
},
"options": {
"type": "object",
"properties": {
"billingSsn": {
"type": "string"
}
},
"$id": "CustomerOption"
}
}
},
"CustomerOption": {
"type": "object",
"properties": {
"billingSsn": {
"type": "string"
}
}
}
},
"requestBodies": {
"CreateCustomerPayload": {
"description": "Create customer payload",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"customer": {
"title": "Customer",
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"name": {
"type": "string",
"nullable": true,
"example": "Albert Albertsson"
},
"options": {
"type": "object",
"properties": {
"billingSsn": {
"type": "string"
}
},
"$id": "CustomerOption"
}
},
"$id": "Customer"
}
},
"required": [
"customer"
]
}
}
}
}
}
}
}
Loading
Loading