Skip to content

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

Open
@MagnusJohansson

Description

@MagnusJohansson

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions