Skip to content

Commit

Permalink
Add docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SOHELAHMED7 committed Sep 23, 2024
1 parent b5c87a4 commit fc75f0b
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 5 deletions.
6 changes: 3 additions & 3 deletions TODO.taskpaper
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
TODO.taskpaper

create failing test
implement the solution
fix failing tests if any
create failing test @done (24-09-23 18:50)
implement the solution @done (24-09-23 18:50)
fix failing tests if any @done (24-09-23 18:50)
☐ resolve TODOs if any
☐ undo commit https://github.com/cebe/php-openapi/pull/208/commits/27e2618787ac21275cb2137e06bd35f202b51b6b
☐ undo commit https://github.com/cebe/php-openapi/pull/208/commits/b4c19c7599154029dc2035d42282b05237124d79
Expand Down
69 changes: 69 additions & 0 deletions doc/array merge recursive distinct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
### array merge recursive distinct

While resolving `allOf`s (pull request https://github.com/cebe/php-openapi/pull/208), if a duplicate property is found

```yaml
components:
schemas:
User:
type: object
required:
- id
- name # <--------------------------------------------------------------
properties:
id:
type: integer
name: # <--------------------------------------------------------------
type: string
maxLength: 10 # <--------------------------------------------------------------
Pet:
type: object
required:
- id2
- name # <--------------------------------------------------------------
properties:
id2:
type: integer
name: # <--------------------------------------------------------------
type: string
maxLength: 12 # <--------------------------------------------------------------
Post:
type: object
properties:
id:
type: integer
content:
type: string
user:
allOf:
- $ref: '#/components/schemas/User'
- $ref: '#/components/schemas/Pet'
- x-faker: true
```
then property from the last component schema will be considered:
```yaml
Post:
type: object
properties:
id:
type: integer
content:
type: string
user:
type: object
required:
- id
- name # <--------------------------------------------------------------
- id2
properties:
id:
type: integer
name: # <--------------------------------------------------------------
type: string
maxLength: 12 # <--------------------------------------------------------------
id2:
type: integer
x-faker: true
```
2 changes: 1 addition & 1 deletion src/spec/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function performValidation()
* unique
*
* Function call example: `$result = array_merge_recursive_distinct(a1, a2, ... aN);`
* TODO add test and more docs
* More documentation is present at [array merge recursive distinct.md](../../../doc/array merge recursive distinct.md) file
*/
public static function arrayMergeRecursiveDistinct()
{
Expand Down
83 changes: 83 additions & 0 deletions tests/spec/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,89 @@ public function testSpecs($openApiFile)
if ($openapi->externalDocs !== null) {
$this->assertInstanceOf(\cebe\openapi\spec\ExternalDocumentation::class, $openapi->externalDocs);
}
}

public function testArrayMergeRecursiveDistinct()
{
$result = OpenApi::arrayMergeRecursiveDistinct(['id', 'name'], ['id2', 'name2']);
$this->assertSame(['id', 'name', 'id2', 'name2'], $result);

$result = OpenApi::arrayMergeRecursiveDistinct(['id', 'name'], ['id2', 'name']);
$this->assertSame(['id', 'name', 'id2'], $result);

$result = OpenApi::arrayMergeRecursiveDistinct(['type' => 'object'], ['x-faker' => true]);
$this->assertSame(['type' => 'object', 'x-faker' => true], $result);

$result = OpenApi::arrayMergeRecursiveDistinct([
'properties' => [
'id' => [
'type' => 'integer'
],
'name' => [
'type' => 'string'
],
]
], [
'properties' => [
'id2' => [
'type' => 'integer'
],
'name2' => [
'type' => 'string'
],
]
]);
$this->assertSame([
'properties' => [
'id' => [
'type' => 'integer'
],
'name' => [
'type' => 'string'
],
'id2' => [
'type' => 'integer'
],
'name2' => [
'type' => 'string'
],
]
], $result);

$result = OpenApi::arrayMergeRecursiveDistinct([
'properties' => [
'id' => [
'type' => 'integer'
],
'name' => [
'type' => 'string',
'maxLength' => 10
],
]
], [
'properties' => [
'id2' => [
'type' => 'integer'
],
'name' => [
'type' => 'string',
'maxLength' => 12
],
]
]);
$this->assertSame([
'properties' => [
'id' => [
'type' => 'integer'
],
'name' => [
'type' => 'string',
'maxLength' => 12
],
'id2' => [
'type' => 'integer'
]
]
], $result);
}
}
1 change: 0 additions & 1 deletion tests/spec/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ public function testPropertyNameRef()
}

// https://github.com/cebe/yii2-openapi/issues/165
// TODO cleanup
public function test165ResolveAllOf()
{
$openApi = Reader::readFromYamlFile(__DIR__ . '/data/resolve_all_of.yml', OpenApi::class, true, true);
Expand Down

0 comments on commit fc75f0b

Please sign in to comment.