Skip to content

Commit b848fae

Browse files
authored
Merge pull request #19 from ensi-platform/task-100616
#100616
2 parents 3c32930 + 2a3903a commit b848fae

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

src/Data/OpenApi3/OpenApi3Object.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ public function fillFromStdObject(stdClass $object): void
2424
/** @var OpenApi3ObjectProperty $objectProperty */
2525
$objectProperty = $this->properties->get($propertyName);
2626
if (!$objectProperty) {
27-
$objectProperty = new OpenApi3ObjectProperty(type: $property->type, name: $propertyName);
27+
do_with_all_of($property, function (stdClass $p) use (&$objectProperty, $propertyName) {
28+
if (!$objectProperty && std_object_has($p, 'type')) {
29+
$objectProperty = new OpenApi3ObjectProperty(type: $p->type, name: $propertyName);
30+
}
31+
});
32+
if (!$objectProperty) {
33+
continue;
34+
}
2835
$this->properties->put($propertyName, $objectProperty);
2936
}
30-
$objectProperty->fillFromStdProperty($propertyName, $property);
37+
do_with_all_of($property, function (stdClass $p) use ($objectProperty, $propertyName) {
38+
$objectProperty->fillFromStdProperty($propertyName, $p);
39+
});
3140
}
3241
}
3342
if (std_object_has($object, 'required') && is_array($object->required)) {

src/Data/OpenApi3/OpenApi3ObjectProperty.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,26 @@ public function fillFromStdProperty(string $propertyName, stdClass $stdProperty)
3434
$this->enumClass = $stdProperty->{'x-lg-enum-class'};
3535
}
3636

37-
switch (OpenApi3PropertyTypeEnum::from($stdProperty->type)) {
38-
case OpenApi3PropertyTypeEnum::OBJECT:
39-
$this->object = new OpenApi3Object();
40-
$this->object->fillFromStdObject($stdProperty);
41-
42-
break;
43-
case OpenApi3PropertyTypeEnum::ARRAY:
44-
if (std_object_has($stdProperty, 'items')) {
45-
if (std_object_has($stdProperty->items, 'type')) {
46-
$this->items = new OpenApi3ObjectProperty(type: $stdProperty->items->type);
47-
$this->items->fillFromStdProperty("{$propertyName}.*", $stdProperty->items);
48-
} elseif (std_object_has($stdProperty->items, 'allOf')) {
49-
foreach ($stdProperty->items->allOf as $allOfItem) {
50-
if (!$this->items && std_object_has($allOfItem, 'type')) {
51-
$this->items = new OpenApi3ObjectProperty(type: $allOfItem->type);
37+
if (std_object_has($stdProperty, 'type')) {
38+
switch (OpenApi3PropertyTypeEnum::from($stdProperty->type)) {
39+
case OpenApi3PropertyTypeEnum::OBJECT:
40+
$this->object = new OpenApi3Object();
41+
$this->object->fillFromStdObject($stdProperty);
42+
43+
break;
44+
case OpenApi3PropertyTypeEnum::ARRAY:
45+
if (std_object_has($stdProperty, 'items')) {
46+
do_with_all_of($stdProperty->items, function (stdClass $p) use ($propertyName) {
47+
if (!$this->items && std_object_has($p, 'type')) {
48+
$this->items = new OpenApi3ObjectProperty(type: $p->type);
5249
}
53-
$this->items?->fillFromStdProperty("{$propertyName}.*", $allOfItem);
54-
}
50+
$this->items?->fillFromStdProperty("{$propertyName}.*", $p);
51+
});
5552
}
56-
}
5753

58-
break;
59-
default:
54+
break;
55+
default:
56+
}
6057
}
6158
}
6259

src/Data/OpenApi3/OpenApi3Schema.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ public function fillFromStdRequestBody(OpenApi3ContentTypeEnum $contentType, std
2020
switch ($contentType) {
2121
case OpenApi3ContentTypeEnum::APPLICATION_JSON:
2222
$schema = $requestBody->content->{OpenApi3ContentTypeEnum::APPLICATION_JSON->value}->schema;
23-
if (std_object_has($schema, 'allOf')) {
24-
foreach ($schema->allOf as $object) {
25-
$this->object->fillFromStdObject($object);
26-
}
27-
} else {
28-
$this->object->fillFromStdObject($schema);
29-
}
23+
do_with_all_of($schema, function (stdClass $p) {
24+
$this->object->fillFromStdObject($p);
25+
});
3026

3127
break;
3228
case OpenApi3ContentTypeEnum::MULTIPART_FROM_DATA:

src/helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@ function std_object_has(stdClass $object, string $propertyName): bool
66
return isset(get_object_vars($object)[$propertyName]);
77
}
88
}
9+
10+
if (!function_exists('do_with_all_of')) {
11+
function do_with_all_of(stdClass $object, callable $fn): void
12+
{
13+
$fn($object);
14+
if (std_object_has($object, 'allOf')) {
15+
foreach ($object->allOf as $allOfItem) {
16+
$fn($allOfItem);
17+
}
18+
}
19+
}
20+
}

tests/expects/LaravelValidationsApplicationJsonRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'field_integer_double_fillable' => ['integer'],
3939
'field_integer_fillable' => ['integer'],
4040
'field_object_readonly.field' => ['integer'],
41+
'field_allOf_readonly' => [new Enum(TestStringEnum::class)],
4142
'field_array_allOf_readonly' => ['array'],
4243
'field_array_allOf_readonly.*' => [new Enum(TestStringEnum::class)],
4344
'field_array_readonly' => ['array'],

tests/resources/schemas/resources.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ ResourceReadonlyForTestValidationRules:
3232
- type: string
3333
- type: string
3434
x-lg-enum-class: 'TestStringEnum'
35+
field_allOf_readonly:
36+
allOf:
37+
- type: string
38+
- type: string
39+
x-lg-enum-class: 'TestStringEnum'
3540
field_object_readonly:
3641
type: object
3742
description: Поле типа object

0 commit comments

Comments
 (0)