Skip to content

[core] host_pipeline 'body as T' throws raw TypeError on response-type mismatch #39

Description

@cemal-yilmaz-bt

Summary

HostPipeline returns the response with body: parsedBody as T, a runtime cast. When the server's actual body type doesn't match the caller's declared T, this throws an unhandled TypeError instead of returning a handled response/error.

File: packages/dart/morph_core/lib/src/http/host_pipeline.dart:223

Root cause

  • parsedBody is typed Object? (L184) and JSON responses are decoded into Map/List (L192, jsonDecode).
  • The success path returns body: parsedBody as T (L223).
  • If a consumer declares MorphResponse<String> but the endpoint returns content-type: application/json (parsed to a Map), or declares MorphResponse<Map> but the body is a bare JSON array or a non-JSON String, the cast throws a raw TypeError.

Why this is a Dart-specific regression

In the TypeScript source parsed as T is a compile-time-only assertion and never throws at runtime. The Dart as T is checked at runtime, so a response the TS SDK handled fine now crashes.

Repro

A 200 endpoint that a consumer typed as MorphResponse<String> returns content-type: application/json with {"x":1} → crash with an opaque cast error rather than a MorphResponse/MorphHttpError.

Proposed fix

Guard the cast (e.g. is T check → surface a typed decode error or a safe fallback), or return a dynamic/Object? body and let callers coerce, or emit a MorphHttpError/decode error on type mismatch instead of an unguarded cast.

Severity

High — crashes on an otherwise-valid server response.

Found in a local review of PR #37 (head fdbce85).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions