Skip to content

Commit

Permalink
fix(OpenApiType): Use anyOf instead of oneOf for conflicting types
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Apr 10, 2024
1 parent 6bfd37f commit 61f492c
Show file tree
Hide file tree
Showing 5 changed files with 737 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,30 @@ enum: $values,
return $type;
}

if ($isIntersection) {
return new OpenApiType(
nullable: $nullable,
allOf: $items,
);
}

$itemTypes = array_map(static function (OpenApiType $item) {
if ($item->type === 'integer') {
return 'number';
}
return $item->type;
}, $items);

if (!empty(array_filter($itemTypes, static fn (?string $type) => $type === null)) || count($itemTypes) !== count(array_unique($itemTypes))) {
return new OpenApiType(
nullable: $nullable,
anyOf: $items,
);
}

return new OpenApiType(
nullable: $nullable,
oneOf: $isUnion ? $items : null,
allOf: $isIntersection ? $items : null,
oneOf: $items,
);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@
['name' => 'Settings#empty304', 'url' => '/api/{apiVersion}/304', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#passwordConfirmationAnnotation', 'url' => '/api/{apiVersion}/passwordConfirmationAnnotation', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#passwordConfirmationAttribute', 'url' => '/api/{apiVersion}/passwordConfirmationAttribute', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#oneOf', 'url' => '/api/{apiVersion}/oneOf', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#anyOf', 'url' => '/api/{apiVersion}/anyOf', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
],
];
27 changes: 27 additions & 0 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,31 @@ public function passwordConfirmationAnnotation(): DataResponse {
public function passwordConfirmationAttribute(): DataResponse {
return new DataResponse();
}

/**
* Route with oneOf
*
* @return DataResponse<Http::STATUS_OK, string|int|double|bool, array{}>
*
* 200: OK
*/
#[PasswordConfirmationRequired]
public function oneOf(): DataResponse {
return new DataResponse();
}

/**
* Route with anyOf
*
* @return DataResponse<Http::STATUS_OK, array{test: string}|array{test: string, abc: int}, array{}>|DataResponse<Http::STATUS_CREATED, array{foobar: string}|array{disco: string, abc: int}|array{test: string, abc: int}, array{}>|DataResponse<Http::STATUS_ACCEPTED, float|double, array{}>|DataResponse<Http::STATUS_RESET_CONTENT, int|double, array{}>
*
* 200: OK
* 201: CREATED
* 202: ACCEPTED
* 205: RESET CONTENT
*/
#[PasswordConfirmationRequired]
public function anyOf(): DataResponse {
return new DataResponse();
}
}
Loading

0 comments on commit 61f492c

Please sign in to comment.