Skip to content
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
merged 12 commits into from
Jan 2, 2024
Merged
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())) {
Copy link
Collaborator

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

Copy link
Collaborator

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()

throw new UnexpectedResponseBodyException();
}

return $schema;
}
}
72 changes: 72 additions & 0 deletions test/suite/functional/Generator/SchemaMapper/anyOf.yaml
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
14 changes: 10 additions & 4 deletions test/suite/functional/Generator/SchemaMapperGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(),
],
Expand Down
Loading