Skip to content
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

When the response code is 204, it tries to de-serialize an empty body #74

Open
MagnusJohansson opened this issue Apr 7, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@MagnusJohansson
Copy link

Hi,

given an APi endpoint that responds with either a 200 or 204 (Non content)

    "responses": {
                    "200": {
                        "description": "Success",
                        "content": {
                            "text/plain": {
                                "schema": {
                                    "$ref": "#/components/schemas/MarketingDocumentContract"
                                }
                            },
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/MarketingDocumentContract"
                                }
                            },
                            "text/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/MarketingDocumentContract"
                                }
                            }
                        }
                    },
                    "204": {
                        "description": "No Content"
                    },
                    "400": {
                        "description": "Bad Request",
                        "content": {
                            "text/plain": {
                                "schema": {
                                    "$ref": "#/components/schemas/ValidationProblemDetails"
                                }
                            },
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ValidationProblemDetails"
                                }
                            },
                            "text/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ValidationProblemDetails"
                                }
                            }
                        }
                    }
                }

The generated code will try to de-serialize an empty body (the last line):

 ///
  ///
  /// Download a marketing PDF
  Future<MarketingDocumentContract> generateMarketingPdf(
      {Options? options,
      MarketingDocumentRequestContract?
          marketingDocumentRequestContract}) async {
    final response = await apiDelegate.generateMarketingPdf(
        options: options,
        marketingDocumentRequestContract: marketingDocumentRequestContract);

    if (![200, 204, 400].contains(response.statusCode)) {
      throw ApiException(500,
          'Invalid response code ${response.statusCode} returned from API');
    }

    final __body = response.body;
    if (response.statusCode >= 400) {
      throw ApiException(response.statusCode,
          __body == null ? null : await decodeBodyBytes(__body));
    }

    if (__body == null) {
      throw ApiException(500, 'Received an empty body (not in a 204)');
    }

    return await apiDelegate.generateMarketingPdf_decode(__body);
  }

  ///
  ///
  /// Download a marketing PDF
}

Which will result in:

[log] FormatException: Unexpected end of input (at character 1)
      
      ^
E/flutter ( 7505): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
E/flutter ( 7505):
E/flutter ( 7505): ^
E/flutter ( 7505):
E/flutter ( 7505): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
E/flutter ( 7505): #1      _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:523:7)
E/flutter ( 7505): #2      _parseJson (dart:convert-patch/convert_patch.dart:41:10)
E/flutter ( 7505): #3      JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 7505): #4      JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter ( 7505): #5      jsonDecode (dart:convert/json.dart:155:10)
E/flutter ( 7505): #6      LocalApiClient.deserializeFromString
@rvowles
Copy link
Member

rvowles commented Apr 7, 2022

Hrm - when you are returning 3 different things from one API, you shouldn't really use this method. Dart doesn't have the capability like Typescript where you can return multiple different types from one function. I would recommend as a workaround that you just use the apiDelegate directly (it is exposed for just this kind of use case).

To better support this kind of use case, I should really be returning a generated class which holds all the possible return types.

@rvowles rvowles added the enhancement New feature or request label May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants