Swagger-codegen: PHP SDK codegen exception for 'default' response

Created on 9 May 2017  路  7Comments  路  Source: swagger-api/swagger-codegen

Description

[PHP] Bug generating *Api::*WithHttpInfo()

For default response in the schema, TestApi::testWithHttpInfo() is generated as:

try {
  list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(.......);
  return [..., $statusCode, $httpHeader];
} catch (ApiException $e) {
  switch ($e->getCode()) {
    case 200:
      $data = ...; break;
    case 0:
      $data = ...; break;
  }
  throw $e;
}

Should it not be like this one:

switch ($e->getCode()) {
  case 200:
    $data = ...; break;
  default:
    $data = ...; break;
}
Swagger-codegen version

2.2.2

Swagger declaration file content or url

https://pastebin.com/fcwebCHe

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i resources/schema/spec.json -l php -Dmodel -o sdk/php

Steps to reproduce
Related issues
Suggest a Fix

Thanks :)

PHP Bug

All 7 comments

@adhocore I believe non-2xx response will result in ApiException.

i mean it is not about 2xx it is about case 0 please see the generated code section:

switch ($e->getCode()) {
    case 200:
      $data = ...; break;
    case 0:
      $data = ...; break;
}

there is very slim chance that non 2xx code is but 0. 0 is not even http status code. since we are defaulting anything except 200 to be caught as error, should it not be

switch ($e->getCode()) {
  case 200:
    $data = ...; break;
  default:
    $data = ...; break;
}

The code usually refers to HTTP status code but it can contain 0 if my memory serves me well for the situation in which the error happens in the client side (e.g. network issue that the client cannot reach the server)

that is network disconnected issue but is this the only case for defaulting? ofcourse no. all errors be it 0 or 4xx or 5xx are caught by default: section and it resonates to the schema too. but 0? :)

@adhocore I think I know what you mean now. 'defaultresponse was incorrectly shown as 0. Like what you said, it should be mapped to thedefault` switch case.

Would you have time to contribute the fix? If yes, I can show you some good starting points.

I just had a look at this and it seems @wing328 fixed this earlier today in commit ef35b80bd873281266389941382a6ecea65eb17a.

@adhocore let us know if you still see issues in the latest master.

Was this page helpful?
0 / 5 - 0 ratings