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

ResponseConverterInterceptor overrides null body #622

Closed
flodaniel opened this issue Jun 20, 2024 · 2 comments · Fixed by #623
Closed

ResponseConverterInterceptor overrides null body #622

flodaniel opened this issue Jun 20, 2024 · 2 comments · Fixed by #623
Labels
bug Something isn't working question Further information is requested

Comments

@flodaniel
Copy link

Steps to Reproduce

  1. Doing a GET request that returns "" (an empty response), I want to convert it to null, which is done by my own JsonConverter implementation:

Service Implementation:

@ChopperApi()
abstract class AppUpdateInformationService extends ChopperService {
  @Get(headers: {'content-type': 'application/json'})
  Future<Response<AppUpdateInformationResponse?>> getAppUpdateInformation(
      {@Query('appVersion') int appVersion = 0,
      @Query('platform') String platform = ''});

  static AppUpdateInformationService create(AuthenticationRepository authRepo) {
    final client = ServiceHelper.getChopperClient(
      servicePath: 'app-update-information',
      converter: JsonToTypeConverter({
        AppUpdateInformationResponse: AppUpdateInformationResponse.fromJson
      }),
      authenticator: PaveRefreshTokenAuthenticator(authRepo),
      services: [_$AppUpdateInformationService()],
    );

    return _$AppUpdateInformationService(client);
  }
}

JsonConverter

 @override
  Response<BodyType> convertResponse<BodyType, InnerType>(Response response) {
    try {
      var convertedBody = response.body;
      if (response.body is String && InnerType != String) {
        convertedBody = fromJsonData<BodyType, InnerType>(
          response.body,
          typeToJsonFactoryMap[InnerType],
        );
      }

      if (convertedBody == null) {
        return Response<BodyType>(response.base, null, error: response.error);
      }

      return response.copyWith<BodyType>(body: convertedBody);
    } catch (e, stackTrace) {
      SentryService().recordError(
        '''Failed to convert response for BodyType $BodyType and InnerType $InnerType''',
        stackTrace: stackTrace,
        stringHint: response.bodyString,
      );
      rethrow;
    }
  }

It returns Response<BodyType>(response.base, null, error: response.error);

  1. The in v8 introduced ResponseConverterInterceptor ignores the null response and tries to use the "" (empty string) response on line 64:
    return Response<BodyType>(
      newResponse?.base ?? response.base,
      newResponse?.body ?? response.body,
    );

Should I use a different approach to this or is this a bug?

@flodaniel flodaniel added the bug Something isn't working label Jun 20, 2024
@techouse techouse assigned techouse and unassigned techouse Jun 21, 2024
@techouse techouse added the question Further information is requested label Jun 21, 2024
@techouse
Copy link
Collaborator

techouse commented Jun 21, 2024

@Guldem can probably shed some light on this.

I would maybe suggest using a different approach and always returning a JSON payload with some empty state or returning an HTTP/204 (No Content) status and then intercepting that status rather than an looking for an empty string.

@Guldem
Copy link
Contributor

Guldem commented Jun 25, 2024

While I agree with @techouse that I'm not sure if the approach is right. I do also thing this is a small bug.

A Response converter should be able to change the body of the response including null. I guess I have overlooked that part initially.

I have submitted a fix #623

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants