-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add oneOf schema support #103
Merged
vsouz4
merged 12 commits into
DoclerLabs:master
from
oleg-docler:one-of-with-discriminator
Jan 2, 2024
+1,779
−81
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2ea37bb
feat(makefile): add build target
dmolnar-byborgenterprises 6827b08
feat: add `oneOf` schema support
dmolnar-byborgenterprises 052e66a
oneOf support
cb338bc
discriminator support
d8fa4ed
Merge remote-tracking branch 'origin/master' into fork-master
2af3b3e
make discriminator optional
cd67a66
import UnexpectedResponseBodyException
b162168
fix oneOf and anyOf support
e7d0e8c
anyOf test
cb77c34
using $matches
c3b5d2d
using php 8
55ec4a9
new tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
test/suite/functional/Generator/SchemaMapper/AnyOfResponseBodyMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file was generated by docler-labs/api-client-generator. | ||
* | ||
* Do not edit it manually. | ||
*/ | ||
|
||
namespace Test\Schema\Mapper; | ||
|
||
use DoclerLabs\ApiClientException\UnexpectedResponseBodyException; | ||
use Test\Schema\GetExampleResponseBody; | ||
|
||
class GetExampleResponseBodyMapper implements SchemaMapperInterface | ||
{ | ||
private AnimalMapper $animalMapper; | ||
|
||
private MachineMapper $machineMapper; | ||
|
||
public function __construct(AnimalMapper $animalMapper, MachineMapper $machineMapper) | ||
{ | ||
$this->animalMapper = $animalMapper; | ||
$this->machineMapper = $machineMapper; | ||
} | ||
|
||
/** | ||
* @throws UnexpectedResponseBodyException | ||
*/ | ||
public function toSchema(array $payload): GetExampleResponseBody | ||
{ | ||
$schema = new GetExampleResponseBody(); | ||
|
||
try { | ||
$schema->setAnimal($this->animalMapper->toSchema($payload)); | ||
} catch (UnexpectedResponseBodyException $exception) { | ||
} | ||
|
||
try { | ||
$schema->setMachine($this->machineMapper->toSchema($payload)); | ||
} catch (UnexpectedResponseBodyException $exception) { | ||
} | ||
if (empty($schema->toArray())) { | ||
throw new UnexpectedResponseBodyException(); | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: OneOf with Nested Structures | ||
version: 1.0.0 | ||
|
||
paths: | ||
/example: | ||
get: | ||
responses: | ||
'200': | ||
description: An example of an oneOf structure | ||
content: | ||
application/json: | ||
schema: | ||
anyOf: | ||
- $ref: '#/components/schemas/Animal' | ||
- $ref: '#/components/schemas/Machine' | ||
|
||
components: | ||
schemas: | ||
Animal: | ||
type: object | ||
required: | ||
- objectType | ||
- name | ||
properties: | ||
objectType: | ||
type: string | ||
name: | ||
type: string | ||
characteristics: | ||
anyOf: | ||
- $ref: '#/components/schemas/Mammal' | ||
- $ref: '#/components/schemas/Bird' | ||
Mammal: | ||
type: object | ||
required: | ||
- type | ||
- furColor | ||
properties: | ||
type: | ||
type: string | ||
furColor: | ||
type: string | ||
Bird: | ||
type: object | ||
required: | ||
- type | ||
- wingSpan | ||
properties: | ||
type: | ||
type: string | ||
enum: [bird] | ||
wingSpan: | ||
type: integer | ||
Machine: | ||
type: object | ||
required: | ||
- objectType | ||
- model | ||
properties: | ||
objectType: | ||
type: string | ||
model: | ||
type: string | ||
specifications: | ||
type: object | ||
properties: | ||
power: | ||
type: integer | ||
year: | ||
type: integer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,15 +64,21 @@ public function exampleProvider(): array | |
self::BASE_NAMESPACE . SchemaMapperGenerator::NAMESPACE_SUBPATH . '\\FreeFormItemMapper', | ||
ConfigurationBuilder::fake()->withPhpVersion(PhpVersion::VERSION_PHP80)->build(), | ||
], | ||
'Complex OneOf response' => [ | ||
'OneOf response' => [ | ||
'/SchemaMapper/oneOf.yaml', | ||
'/SchemaMapper/GetExampleResponseBodyMapper.php', | ||
'/SchemaMapper/OneOfResponseBodyMapper.php', | ||
self::BASE_NAMESPACE . SchemaMapperGenerator::NAMESPACE_SUBPATH . '\\GetExampleResponseBodyMapper', | ||
ConfigurationBuilder::fake()->build(), | ||
], | ||
'Complex OneOf response without discriminator' => [ | ||
'OneOf response without discriminator' => [ | ||
'/SchemaMapper/oneOfWithoutDiscriminator.yaml', | ||
'/SchemaMapper/GetExampleResponseBodyMapperWithoutDiscriminator.php', | ||
'/SchemaMapper/OneOfResponseBodyMapperWithoutDiscriminator.php', | ||
self::BASE_NAMESPACE . SchemaMapperGenerator::NAMESPACE_SUBPATH . '\\GetExampleResponseBodyMapper', | ||
ConfigurationBuilder::fake()->build(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please extend the all these generator tests you added with the php 8.0 version too? |
||
], | ||
'AnyOf response' => [ | ||
'/SchemaMapper/anyOf.yaml', | ||
'/SchemaMapper/AnyOfResponseBodyMapper.php', | ||
self::BASE_NAMESPACE . SchemaMapperGenerator::NAMESPACE_SUBPATH . '\\GetExampleResponseBodyMapper', | ||
ConfigurationBuilder::fake()->build(), | ||
], | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if we add a $matches = 0 before all the tries, and inside the try after the $schema->set... we $matches ++
we know when generating this Mapper if it's oneOf or anyOf, so we can already add the code when it's anyOf, we check if ($matches === 0) throw UnexpectedResponseBodyException, when it's oneOf we check if $matches !== 1 throw UnexpectedResponseBodyException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and then we can remove this if (empty(toarray()