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
60 changes: 51 additions & 9 deletions src/Service/InlineService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
private array $components = [];
/** @var list<string> */
private array $allOfPaths = [];
/** @var array<string, list<string>> */
private array $modelAllOfPaths = [];

private readonly DocumentFactory $documentFactory;
private LocalReferenceResolver $localReferenceResolver;
Expand Down Expand Up @@ -97,13 +99,15 @@
private function processDocument(
Document&WritableDocument $document,
?Reference $rootReference = null,
?string $parentPath = null,
): Document&WritableDocument {
$isAllOfDocument = ($document->get()['allOf'] ?? null) !== null;
foreach ($this->findReferences($document, $rootReference) as $ref => $documentPaths) {
$reference = Reference::fromString($ref);
$type = $this->resolveSchemaType($documentPaths);
$refName = $this->getRefName($reference, $type);

$nextParentPath = $parentPath;
if (
isset($this->components[$type->name][$refName])
&& $this->components[$type->name][$refName]
Expand All @@ -112,9 +116,14 @@
if (is_string($schema)) {
$schema = ['$ref' => $schema];
}
foreach ($documentPaths as $path) {
$document->set($path, $schema);
}
$document = $this->processDocumentPaths(
$document,
$schema,
$documentPaths,
$refName,
$rootReference?->getName(),
$parentPath,
);
continue;
}

Expand All @@ -130,32 +139,65 @@
}
else {
$referenceDocument = BasicDocument::fromDocument($this->referenceResolver->resolve($reference));
$nextParentPath .= $documentPaths[0];
}
$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,
$reference,
$nextParentPath,
)->get();
$this->components[$type->name][$refName]['$id'] = $schemaId;

// update $ref to new ref
foreach ($documentPaths as $path) {
if (str_contains($path, '/allOf/')) {
$this->allOfPaths[] = substr($path, 0, (int)strpos($path, '/allOf/'));
}
$document->set($path, $this->components[$type->name][$refName]);
}
$document = $this->processDocumentPaths(
$document,
$this->components[$type->name][$refName],
$documentPaths,
$refName,
$rootReference?->getName(),
$parentPath,
);
}
if ($isAllOfDocument) {
$merger = new AllOfMerger($document);
$mergedSchema = $merger->merge('/allOf');
return $this->documentFactory->createFromArray($document->getId(), $mergedSchema);

Check failure on line 167 in src/Service/InlineService.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.2)

Parameter #2 $data of method JsonPointer\DocumentFactory::createFromArray() expects array{'$id'?: string, id?: string}, array<string, mixed> given.

Check failure on line 167 in src/Service/InlineService.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.2)

Parameter #2 $data of method JsonPointer\DocumentFactory::createFromArray() expects array{'$id'?: string, id?: string}, array<string, mixed> given.
}
return $document;
}

/**
* @param list<string> $documentPaths
*/
private function processDocumentPaths(
Document&WritableDocument $document,
mixed $schema,
array $documentPaths,
string $refName,
?string $parentName = null,
?string $parentPath = null,
): Document&WritableDocument {
$parentName ??= 'root';
foreach ($documentPaths as $path) {
if (array_key_exists($refName, $this->modelAllOfPaths)) {
foreach ($this->modelAllOfPaths[$refName] as $allOfPath) {
$this->allOfPaths[] = ($parentPath ?? '') . $path . $allOfPath;
}
}
elseif (str_contains($path, '/allOf/')) {
$stripped = substr($path, 0, (int)strpos($path, '/allOf/'));
$this->modelAllOfPaths[$parentName][] = $stripped;
$this->allOfPaths[] = $parentPath . $stripped;
}
$document->set($path, $schema);
}

return $document;
}

/**
* @return array<string, list<string>>
*/
Expand Down
145 changes: 145 additions & 0 deletions tests/inlined-schemas/inline-allOf-multiple/schema.dist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"title": "Account",
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"name": {
"type": "string"
},
"status": {
"type": "string",
"enum": [
"new",
"verified",
"active",
"blocked",
"deleted"
],
"readOnly": true
},
"createdOn": {
"type": "string",
"format": "date-time",
"readOnly": true
},
"anotherRefWithAllOf": {
"$id": "OtherNestedAllOf.json",
"properties": {
"otherNestedAllOf": {
"properties": {
"sub1": {
"type": "string"
},
"sub2": {
"format": "uuid",
"type": "string"
}
},
"readOnly": true,
"required": [
"sub1"
],
"type": "object"
}
},
"type": "object"
},
"blockedUntil": {
"type": "string",
"format": "date-time"
},
"role": {
"readOnly": true,
"type": "object",
"properties": {
"name": {
"type": "string"
},
"sub2": {
"type": "string"
},
"externalId": {
"type": "string",
"format": "uuid"
}
}
},
"complexAllOfMerge": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"sub2": {
"type": "string",
"format": "uuid"
},
"externalId": {
"type": "string",
"format": "uuid"
},
"sub1": {
"type": "string"
},
"extraFields": {
"type": "string"
}
},
"required": [
"sub1",
"extraFields"
]
},
"otherRefWithAllOf": {
"$id": "OtherNestedAllOf.json",
"properties": {
"otherNestedAllOf": {
"properties": {
"sub1": {
"type": "string"
},
"sub2": {
"format": "uuid",
"type": "string"
}
},
"readOnly": true,
"required": [
"sub1"
],
"type": "object"
}
},
"type": "object"
},
"refWithAllOf": {
"$id": "NestedAllOf.json",
"properties": {
"nestedAllOf": {
"properties": {
"sub1": {
"type": "string"
},
"sub2": {
"format": "uuid",
"type": "string"
}
},
"readOnly": true,
"required": [
"sub1"
],
"type": "object"
}
},
"type": "object"
},
"required": [
"id",
"status"
]
}
}
74 changes: 74 additions & 0 deletions tests/inlined-schemas/inline-allOf-multiple/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"title": "Account",
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"name": {
"type": "string"
},
"status": {
"type": "string",
"enum": [
"new",
"verified",
"active",
"blocked",
"deleted"
],
"readOnly": true
},
"createdOn": {
"type": "string",
"format": "date-time",
"readOnly": true
},
"blockedUntil": {
"type": "string",
"format": "date-time"
},
"role": {
"allOf": [
{
"readOnly": true
},
{
"$ref": "sub/Role.json"
}
]
},
"complexAllOfMerge": {
"allOf": [
{
"$ref": "sub/Role.json"
},
{
"$ref": "sub/Sub.json"
},
{
"properties": {
"extraFields": {
"type": "string"
}
},
"required": ["extraFields", "sub1"]
}
]
},
"refWithAllOf": {
"$ref": "sub/NestedAllOf.json"
},
"otherRefWithAllOf": {
"$ref": "sub/OtherNestedAllOf.json"
},
"anotherRefWithAllOf": {
"$ref": "sub/OtherNestedAllOf.json"
},
"required": [
"id",
"status"
]
}
}
15 changes: 15 additions & 0 deletions tests/inlined-schemas/inline-allOf-multiple/sub/NestedAllOf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "object",
"properties": {
"nestedAllOf": {
"allOf": [
{
"$ref": "Sub.json"
},
{
"readOnly": true
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "object",
"properties": {
"otherNestedAllOf": {
"allOf": [
{
"$ref": "Sub.json"
},
{
"readOnly": true
}
]
}
}
}
15 changes: 15 additions & 0 deletions tests/inlined-schemas/inline-allOf-multiple/sub/Role.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"sub2": {
"type": "string"
},
"externalId": {
"type": "string",
"format": "uuid"
}
}
}
13 changes: 13 additions & 0 deletions tests/inlined-schemas/inline-allOf-multiple/sub/Sub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "object",
"properties": {
"sub1": {
"type": "string"
},
"sub2": {
"type": "string",
"format": "uuid"
}
},
"required": ["sub1"]
}
Loading