Skip to content

[php-flight] fix: always set http status in streaming response #18604

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

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenResponse;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.GeneratorMetadata;
Expand Down Expand Up @@ -202,11 +203,14 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
List<CodegenOperation> operationList = operations.getOperation();
operationList.forEach(operation -> {
operation.vendorExtensions.put("x-path", mapToFlightPath(operation.path));
String returnType = operation.responses.stream().filter(r -> r.is2xx && r.dataType != null).map(r -> this.getTypeHint(r.dataType, false, false)).filter(t -> !t.isEmpty()).map(t -> t + "|null").findFirst().orElse("void");
CodegenResponse defaultResponse = operation.responses.stream().filter(r -> r.is2xx && r.dataType != null && !this.getTypeHint(r.dataType, false, false).isEmpty()).findFirst().orElse(null);
String returnType = defaultResponse != null ? this.getTypeHint(defaultResponse.dataType, false, false) + "|null" : "void";
operation.vendorExtensions.put("x-return-type", returnType);
operation.vendorExtensions.put("x-return-type-is-void", returnType.equals("void"));
operation.vendorExtensions.put("x-return-type-comment",
operation.responses.stream().filter(r -> r.is2xx && r.dataType != null).map(r -> this.getTypeHint(r.dataType, true, false)).filter(t -> !t.isEmpty()).map(t -> t + "|null").findFirst().orElse("void"));
operation.vendorExtensions.put("x-return-type-comment", defaultResponse != null ? this.getTypeHint(defaultResponse.dataType, true, false) + "|null" : "void");
operation.vendorExtensions.put("x-default-media-type", defaultResponse != null ? (
defaultResponse.getContent().containsKey("application/json") ? "application/json" : defaultResponse.getContent().keySet().stream().findFirst().orElse(null)) : null);
operation.vendorExtensions.put("x-default-status-code", defaultResponse != null ? defaultResponse.code : operation.responses.stream().filter(r -> !r.isDefault).findFirst().map(r -> r.code).orElse("200"));
operation.vendorExtensions.put("x-nonFormParams", operation.allParams.stream().filter(p -> !p.isFormParam).toArray());

operation.allParams.forEach(param -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace {{apiPackage}};
{{#operation}}
/**
* Operation {{{operationId}}}
* Path: {{{path}}}
*
* Path: `{{{path}}}`
*
{{#summary}}
* {{{summary}}}
Expand All @@ -31,10 +32,11 @@ namespace {{apiPackage}};
throw new \Exception('Not implemented');
}

{{#returnContainer}}
/**
* Operation {{{operationId}}} (stream)
*
* Path: `{{{path}}}`
*
{{#summary}}
* {{{summary}}}
*
Expand All @@ -51,7 +53,6 @@ namespace {{apiPackage}};
{
throw new \Exception('Not implemented');
}
{{/returnContainer}}
{{/operation}}
}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ class RegisterRoutes {
);
{{^vendorExtensions.x-return-type-is-void}}
if ($result === null) {
\Flight::halt(204);
\Flight::halt({{{vendorExtensions.x-default-status-code}}});
} else {
\Flight::json($result);
\Flight::json($result, {{{vendorExtensions.x-default-status-code}}});
}
{{/vendorExtensions.x-return-type-is-void}}
{{#vendorExtensions.x-return-type-is-void}}
\Flight::halt(204);
\Flight::halt({{{vendorExtensions.x-default-status-code}}});
{{/vendorExtensions.x-return-type-is-void}}
});
}
{{#returnContainer}}
if (declaresMethod($reflectionClass, '{{operationId}}Stream')) {
\Flight::route('{{httpMethod}} {{vendorExtensions.x-path}}', function ({{#pathParams}}string ${{paramName}}{{^-last}}, {{/-last}}{{/pathParams}}) use ($handler) {
$r = \Flight::request();
Expand All @@ -47,9 +46,8 @@ class RegisterRoutes {
{{/vendorExtensions.x-nonFormParams}}
);
// ignore return value: streaming expected
})->streamWithHeaders(['Content-Type' => 'application/json']);
})->streamWithHeaders(['status' => {{{vendorExtensions.x-default-status-code}}}{{#vendorExtensions.x-default-media-type}}, 'Content-Type' => '{{{vendorExtensions.x-default-media-type}}}'{{/vendorExtensions.x-default-media-type}}]);
}
{{/returnContainer}}

{{/operation}}
{{/operations}}
Expand Down
113 changes: 105 additions & 8 deletions samples/server/petstore/php-flight/Api/AbstractPetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ abstract class AbstractPetApi

/**
* Operation addPet
* Path: /pet
*
* Path: `/pet`
*
* Add a new pet to the store
*
Expand All @@ -36,9 +37,24 @@ public function addPet(\OpenAPIServer\Model\Pet $pet): \OpenAPIServer\Model\Pet|
throw new \Exception('Not implemented');
}

/**
* Operation addPet (stream)
*
* Path: `/pet`
*
* Add a new pet to the store
*
* @param \OpenAPIServer\Model\Pet $pet Pet object that needs to be added to the store (required)
*
*/
public function addPetStream(\OpenAPIServer\Model\Pet $pet): void
{
throw new \Exception('Not implemented');
}
/**
* Operation deletePet
* Path: /pet/{petId}
*
* Path: `/pet/{petId}`
*
* Deletes a pet
*
Expand All @@ -52,9 +68,25 @@ public function deletePet(int $petId, ?string $apiKey): void
throw new \Exception('Not implemented');
}

/**
* Operation deletePet (stream)
*
* Path: `/pet/{petId}`
*
* Deletes a pet
*
* @param int $petId Pet id to delete (required)
* @param ?string $apiKey (optional)
*
*/
public function deletePetStream(int $petId, ?string $apiKey): void
{
throw new \Exception('Not implemented');
}
/**
* Operation findPetsByStatus
* Path: /pet/findByStatus
*
* Path: `/pet/findByStatus`
*
* Finds Pets by status
*
Expand All @@ -70,6 +102,8 @@ public function findPetsByStatus(array $status): array|null
/**
* Operation findPetsByStatus (stream)
*
* Path: `/pet/findByStatus`
*
* Finds Pets by status
*
* @param array $status Status values that need to be considered for filter (required) (deprecated)
Expand All @@ -81,7 +115,8 @@ public function findPetsByStatusStream(array $status): void
}
/**
* Operation findPetsByTags
* Path: /pet/findByTags
*
* Path: `/pet/findByTags`
*
* Finds Pets by tags
*
Expand All @@ -98,6 +133,8 @@ public function findPetsByTags(array $tags): array|null
/**
* Operation findPetsByTags (stream)
*
* Path: `/pet/findByTags`
*
* Finds Pets by tags
*
* @param array $tags Tags to filter by (required)
Expand All @@ -110,7 +147,8 @@ public function findPetsByTagsStream(array $tags): void
}
/**
* Operation getPetById
* Path: /pet/{petId}
*
* Path: `/pet/{petId}`
*
* Find pet by ID
*
Expand All @@ -123,9 +161,24 @@ public function getPetById(int $petId): \OpenAPIServer\Model\Pet|null
throw new \Exception('Not implemented');
}

/**
* Operation getPetById (stream)
*
* Path: `/pet/{petId}`
*
* Find pet by ID
*
* @param int $petId ID of pet to return (required)
*
*/
public function getPetByIdStream(int $petId): void
{
throw new \Exception('Not implemented');
}
/**
* Operation updatePet
* Path: /pet
*
* Path: `/pet`
*
* Update an existing pet
*
Expand All @@ -138,9 +191,24 @@ public function updatePet(\OpenAPIServer\Model\Pet $pet): \OpenAPIServer\Model\P
throw new \Exception('Not implemented');
}

/**
* Operation updatePet (stream)
*
* Path: `/pet`
*
* Update an existing pet
*
* @param \OpenAPIServer\Model\Pet $pet Pet object that needs to be added to the store (required)
*
*/
public function updatePetStream(\OpenAPIServer\Model\Pet $pet): void
{
throw new \Exception('Not implemented');
}
/**
* Operation updatePetWithForm
* Path: /pet/{petId}
*
* Path: `/pet/{petId}`
*
* Updates a pet in the store with form data
*
Expand All @@ -153,9 +221,24 @@ public function updatePetWithForm(int $petId): void
throw new \Exception('Not implemented');
}

/**
* Operation updatePetWithForm (stream)
*
* Path: `/pet/{petId}`
*
* Updates a pet in the store with form data
*
* @param int $petId ID of pet that needs to be updated (required)
*
*/
public function updatePetWithFormStream(int $petId): void
{
throw new \Exception('Not implemented');
}
/**
* Operation uploadFile
* Path: /pet/{petId}/uploadImage
*
* Path: `/pet/{petId}/uploadImage`
*
* uploads an image
*
Expand All @@ -168,4 +251,18 @@ public function uploadFile(int $petId): \OpenAPIServer\Model\ApiResponse|null
throw new \Exception('Not implemented');
}

/**
* Operation uploadFile (stream)
*
* Path: `/pet/{petId}/uploadImage`
*
* uploads an image
*
* @param int $petId ID of pet to update (required)
*
*/
public function uploadFileStream(int $petId): void
{
throw new \Exception('Not implemented');
}
}
56 changes: 52 additions & 4 deletions samples/server/petstore/php-flight/Api/AbstractStoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ abstract class AbstractStoreApi

/**
* Operation deleteOrder
* Path: /store/order/{orderId}
*
* Path: `/store/order/{orderId}`
*
* Delete purchase order by ID
*
Expand All @@ -36,9 +37,24 @@ public function deleteOrder(string $orderId): void
throw new \Exception('Not implemented');
}

/**
* Operation deleteOrder (stream)
*
* Path: `/store/order/{orderId}`
*
* Delete purchase order by ID
*
* @param string $orderId ID of the order that needs to be deleted (required)
*
*/
public function deleteOrderStream(string $orderId): void
{
throw new \Exception('Not implemented');
}
/**
* Operation getInventory
* Path: /store/inventory
*
* Path: `/store/inventory`
*
* Returns pet inventories by status
*
Expand All @@ -53,6 +69,8 @@ public function getInventory(): void
/**
* Operation getInventory (stream)
*
* Path: `/store/inventory`
*
* Returns pet inventories by status
*
*
Expand All @@ -63,7 +81,8 @@ public function getInventoryStream(): void
}
/**
* Operation getOrderById
* Path: /store/order/{orderId}
*
* Path: `/store/order/{orderId}`
*
* Find purchase order by ID
*
Expand All @@ -76,9 +95,24 @@ public function getOrderById(int $orderId): \OpenAPIServer\Model\Order|null
throw new \Exception('Not implemented');
}

/**
* Operation getOrderById (stream)
*
* Path: `/store/order/{orderId}`
*
* Find purchase order by ID
*
* @param int $orderId ID of pet that needs to be fetched (required)
*
*/
public function getOrderByIdStream(int $orderId): void
{
throw new \Exception('Not implemented');
}
/**
* Operation placeOrder
* Path: /store/order
*
* Path: `/store/order`
*
* Place an order for a pet
*
Expand All @@ -91,4 +125,18 @@ public function placeOrder(\OpenAPIServer\Model\Order $order): \OpenAPIServer\Mo
throw new \Exception('Not implemented');
}

/**
* Operation placeOrder (stream)
*
* Path: `/store/order`
*
* Place an order for a pet
*
* @param \OpenAPIServer\Model\Order $order order placed for purchasing the pet (required)
*
*/
public function placeOrderStream(\OpenAPIServer\Model\Order $order): void
{
throw new \Exception('Not implemented');
}
}
Loading
Loading