Skip to content

Commit e663eaf

Browse files
authored
Merge pull request #103 from oleg-docler/one-of-with-discriminator
Add oneOf schema support
2 parents 393bb25 + 55ec4a9 commit e663eaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1779
-81
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [10.5.0] - 2023.12.22
8+
### Added
9+
- `oneOf` schema support
10+
711
## [10.4.0] - 2023.12.13
812
### Added
913
- Added generated code support for php 8.0

Makefile

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
.PHONY: $(filter-out help, $(MAKECMDGOALS))
22
.DEFAULT_GOAL := help
3+
DOCKER_COMPOSE_BINARY=$(if $(shell command -v docker-compose 2> /dev/null),docker-compose,docker compose)
34

45
help:
56
@echo "\033[33mUsage:\033[0m\n make [target] [arg=\"val\"...]\n\n\033[33mTargets:\033[0m"
67
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[32m%-10s\033[0m %s\n", $$1, $$2}'
78

9+
build: ## install dependencies
10+
$(DOCKER_COMPOSE_BINARY) run php composer install
11+
812
test: ## run test suites
9-
docker-compose up -d --build --remove-orphans
10-
docker-compose run --rm wait -c wiremock:8080 -t 60
11-
docker-compose exec -T php vendor/bin/phpunit -c phpunit.xml.dist --colors=always
12-
docker-compose down
13+
$(DOCKER_COMPOSE_BINARY) up -d --build --remove-orphans
14+
$(DOCKER_COMPOSE_BINARY) run --rm wait -c wiremock:8080 -t 60
15+
$(DOCKER_COMPOSE_BINARY) exec -T php vendor/bin/phpunit -c phpunit.xml.dist --colors=always
16+
$(DOCKER_COMPOSE_BINARY) down
1317

1418
cs: ## fix code style
15-
docker-compose run php vendor/bin/php-cs-fixer fix .
19+
$(DOCKER_COMPOSE_BINARY) run php vendor/bin/php-cs-fixer fix .
1620

1721
stan: ## statically analyse code
18-
docker-compose run php vendor/bin/phpstan --memory-limit=1G analyse
22+
$(DOCKER_COMPOSE_BINARY) run php vendor/bin/phpstan analyse --memory-limit 1G
1923

2024
coverage: ## coverage for pipeline
21-
docker-compose run -e COVERALLS_REPO_TOKEN=${COVERALLS_REPO_TOKEN} -e GITHUB_REF=${GITHUB_REF} -e GITHUB_ACTIONS=${GITHUB_ACTIONS} -e GITHUB_RUN_ID=${GITHUB_RUN_ID} -e GITHUB_EVENT_NAME=${GITHUB_EVENT_NAME} php vendor/bin/php-coveralls -v
25+
$(DOCKER_COMPOSE_BINARY) run -e COVERALLS_REPO_TOKEN=${COVERALLS_REPO_TOKEN} -e GITHUB_REF=${GITHUB_REF} -e GITHUB_ACTIONS=${GITHUB_ACTIONS} -e GITHUB_RUN_ID=${GITHUB_RUN_ID} -e GITHUB_EVENT_NAME=${GITHUB_EVENT_NAME} php vendor/bin/php-coveralls -v

example/PetStoreClient/src/Schema/Mapper/CategoryMapper.php

+7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111
namespace OpenApi\PetStoreClient\Schema\Mapper;
1212

13+
use DoclerLabs\ApiClientException\UnexpectedResponseBodyException;
1314
use OpenApi\PetStoreClient\Schema\Category;
1415

1516
class CategoryMapper implements SchemaMapperInterface
1617
{
18+
/**
19+
* @throws UnexpectedResponseBodyException
20+
*/
1721
public function toSchema(array $payload): Category
1822
{
1923
$schema = new Category();
@@ -23,6 +27,9 @@ public function toSchema(array $payload): Category
2327
if (isset($payload['name'])) {
2428
$schema->setName($payload['name']);
2529
}
30+
if (empty($schema->toArray())) {
31+
throw new UnexpectedResponseBodyException();
32+
}
2633

2734
return $schema;
2835
}

example/PetStoreClient/src/Schema/Mapper/OrderMapper.php

+7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
namespace OpenApi\PetStoreClient\Schema\Mapper;
1212

1313
use DateTimeImmutable;
14+
use DoclerLabs\ApiClientException\UnexpectedResponseBodyException;
1415
use OpenApi\PetStoreClient\Schema\Order;
1516

1617
class OrderMapper implements SchemaMapperInterface
1718
{
19+
/**
20+
* @throws UnexpectedResponseBodyException
21+
*/
1822
public function toSchema(array $payload): Order
1923
{
2024
$schema = new Order();
@@ -36,6 +40,9 @@ public function toSchema(array $payload): Order
3640
if (isset($payload['complete'])) {
3741
$schema->setComplete($payload['complete']);
3842
}
43+
if (empty($schema->toArray())) {
44+
throw new UnexpectedResponseBodyException();
45+
}
3946

4047
return $schema;
4148
}

example/PetStoreClient/src/Schema/Mapper/TagMapper.php

+7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111
namespace OpenApi\PetStoreClient\Schema\Mapper;
1212

13+
use DoclerLabs\ApiClientException\UnexpectedResponseBodyException;
1314
use OpenApi\PetStoreClient\Schema\Tag;
1415

1516
class TagMapper implements SchemaMapperInterface
1617
{
18+
/**
19+
* @throws UnexpectedResponseBodyException
20+
*/
1721
public function toSchema(array $payload): Tag
1822
{
1923
$schema = new Tag();
@@ -23,6 +27,9 @@ public function toSchema(array $payload): Tag
2327
if (isset($payload['name'])) {
2428
$schema->setName($payload['name']);
2529
}
30+
if (empty($schema->toArray())) {
31+
throw new UnexpectedResponseBodyException();
32+
}
2633

2734
return $schema;
2835
}

example/PetStoreClient/src/Schema/Mapper/UserMapper.php

+7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111
namespace OpenApi\PetStoreClient\Schema\Mapper;
1212

13+
use DoclerLabs\ApiClientException\UnexpectedResponseBodyException;
1314
use OpenApi\PetStoreClient\Schema\User;
1415

1516
class UserMapper implements SchemaMapperInterface
1617
{
18+
/**
19+
* @throws UnexpectedResponseBodyException
20+
*/
1721
public function toSchema(array $payload): User
1822
{
1923
$schema = new User();
@@ -41,6 +45,9 @@ public function toSchema(array $payload): User
4145
if (isset($payload['userStatus'])) {
4246
$schema->setUserStatus($payload['userStatus']);
4347
}
48+
if (empty($schema->toArray())) {
49+
throw new UnexpectedResponseBodyException();
50+
}
4451

4552
return $schema;
4653
}

src/Entity/Field.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ class Field
2727

2828
private mixed $default = null;
2929

30+
private mixed $discriminator = null;
31+
3032
public function __construct(
3133
private string $name,
3234
private FieldType $type,
3335
private ConstraintCollection $constraints,
3436
private string $referenceName,
3537
private bool $required,
3638
private bool $nullable,
37-
private bool $additionalProperties
39+
private bool $additionalProperties,
40+
private bool $hasOneOf = false,
41+
private bool $hasAnyOf = false
3842
) {
3943
}
4044

@@ -62,6 +66,9 @@ public function getObjectProperties(): array
6266
return $this->objectProperties;
6367
}
6468

69+
/**
70+
* @param Field[] $objectProperties
71+
*/
6572
public function setObjectProperties(array $objectProperties): self
6673
{
6774
$this->objectProperties = $objectProperties;
@@ -128,6 +135,16 @@ public function isNullable(): bool
128135
return $this->nullable;
129136
}
130137

138+
public function hasOneOf(): bool
139+
{
140+
return $this->hasOneOf;
141+
}
142+
143+
public function hasAnyOf(): bool
144+
{
145+
return $this->hasAnyOf;
146+
}
147+
131148
public function isDate(): bool
132149
{
133150
$isDateFormat = $this->getFormat() === self::FORMAT_DATE || $this->getFormat() === self::FORMAT_DATE_TIME;
@@ -169,6 +186,18 @@ public function setDefault(mixed $default): self
169186
return $this;
170187
}
171188

189+
public function getDiscriminator(): mixed
190+
{
191+
return $this->discriminator;
192+
}
193+
194+
public function setDiscriminator(mixed $discriminator): self
195+
{
196+
$this->discriminator = $discriminator;
197+
198+
return $this;
199+
}
200+
172201
public function getPhpVariableName(): string
173202
{
174203
return CaseCaster::toCamel($this->name);

src/Entity/FieldCollection.php

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public function merge(FieldCollection $fieldCollection): self
4141
return (new self())->set(array_values($unique));
4242
}
4343

44+
/**
45+
* @return Field[]
46+
*/
4447
public function getUniqueByPhpClassName(): array
4548
{
4649
$unique = [];

src/Generator/SchemaGenerator.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,12 @@ private function collectSerializationFields(Field $root, Variable $arrayVariable
312312
}
313313
}
314314

315-
$fieldName = $this->builder->val($propertyField->getName());
316-
$assignStatement = $this->builder->appendToAssociativeArray($arrayVariable, $fieldName, $value);
315+
$fieldName = $this->builder->val($propertyField->getName());
316+
if ($root->hasOneOf()) {
317+
$assignStatement = $this->builder->expr($this->builder->assign($arrayVariable, $value));
318+
} else {
319+
$assignStatement = $this->builder->appendToAssociativeArray($arrayVariable, $fieldName, $value);
320+
}
317321

318322
if ($propertyField->isOptional()) {
319323
$ifCondition = $this->builder->localMethodCall(

0 commit comments

Comments
 (0)