Skip to content

Commit

Permalink
Refactor + fix bug WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SOHELAHMED7 committed Sep 20, 2024
1 parent 99dd8be commit 35a9f90
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
20 changes: 10 additions & 10 deletions src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,29 +534,29 @@ public function getProperties(): array

public function mergeProperties($properties)
{
$this->_properties = array_merge($this->_properties, $properties);
$this->_properties = array_merge_recursive($this->_properties, $properties);
}

public function resolveAllOf($parent = null)
public function resolveAllOf()
{
foreach ($this->_properties as $property => $value) {
if ($property === 'allOf' && !empty($value)) {
$this->_properties[$property] = $this->mergeAllAllOfsInToSingleObj($value);
$this->_properties[$property] = $this->mergeAllAllOfsInToSingleObj();
} elseif ($value instanceof SpecObjectInterface && method_exists($value, 'resolveAllOf')) {
$value->resolveAllOf($this->_properties[$property]);
$value->resolveAllOf();
} elseif (is_array($value)) {
foreach ($value as $k => $item) {
if ($k === 'allOf' && !empty($item)) {
$this->_properties[$property][$k] = $this->mergeAllAllOfsInToSingleObj($item);
} elseif ($item instanceof SpecObjectInterface) {
$item->resolveAllOf($this->_properties[$property][$k]);
$this->_properties[$property][$k] = $this->mergeAllAllOfsInToSingleObj();
} elseif ($item instanceof SpecObjectInterface && method_exists($item, 'resolveAllOf')) {
$item->resolveAllOf();
}
}
}
}
}

public function mergeAllAllOfsInToSingleObj($allOfs): self
public function mergeAllAllOfsInToSingleObj(): self
{
$allOfs = $this->allOf;
/** @var static $first */
Expand Down Expand Up @@ -587,10 +587,10 @@ public function resolveAllOf2() // TODO
foreach ($item as $kIn => $vIn) { // TODO rename var names
if (!empty($vIn->allOf)) {
$tempIn = $vIn->allOf;
$this->_properties[$property][$value][$k2][$kIn] = $tempIn;
$this->_properties[$property][$k2][$kIn] = $tempIn;
}
}
} elseif ($item instanceof SpecObjectInterface) {
} elseif ($item instanceof SpecObjectInterface && method_exists($item, 'resolveAllOf2')) {
$item->resolveAllOf2();
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/spec/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,17 @@ public function testPropertyNameRef()
// TODO cleanup
public function test165ResolveAllOf()
{
// return;
// $openApi->resolveReferences(new \cebe\openapi\ReferenceContext($openApi, 'file:///tmp/openapi.yaml'));
$openApi = Reader::readFromYamlFile(__DIR__ . '/data/resolve_all_of.yml');
$result = $openApi->validate();
// $openApi->resolveAllOf();
$this->assertTrue($result);
$this->assertEquals([], $openApi->getErrors());

$this->assertFalse($openApi->components->schemas['Post']->properties['user']->{'x-faker'});
$this->assertTrue($openApi->components->schemas['Post']->properties['user']->{'x-faker2'});

// $this->assertTrue($openApi->components->schemas['Post']->getSerializableData());
// $openApi->components->schemas['Post']->properties['user']->allOf = ['abc' => 'def'];
// $openApi->components->schemas['Post']->properties['user']->__set('allOf', ['abc' => 'def']);
Expand All @@ -441,7 +445,6 @@ public function test165ResolveAllOf()
// $this->assertTrue($openApi->components->schemas['Post']->getSerializableData());
$this->assertSame(
json_decode(json_encode($openApi->components->schemas['Post']->getSerializableData()), true)

, []
);
}
Expand Down
22 changes: 11 additions & 11 deletions tests/spec/data/resolve_all_of.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ components:
type: integer
name:
type: string
# User2:
# type: object
# required:
# - id2
# - name
# properties:
# id2:
# type: integer
# name:
# type: array
Pet:
type: object
required:
- id2
- name2
properties:
id2:
type: integer
name2:
type: array
Post:
type: object
properties:
Expand All @@ -37,7 +37,7 @@ components:
user:
allOf:
- $ref: '#/components/schemas/User'
# - $ref: '#/components/schemas/User2'
- $ref: '#/components/schemas/Pet'
- x-faker: false
- x-faker2: true

Expand Down

0 comments on commit 35a9f90

Please sign in to comment.