Skip to content

Conversation

@ScriptType
Copy link
Contributor

@ScriptType ScriptType commented Dec 23, 2025

Summary

DRAFT

Fixes_partially #2243 - Angular client now properly handles OpenAPI endpoints with multiple response content types using function overloading.

Problem

When an OpenAPI endpoint defines multiple response content types (e.g., application/json, text/plain, application/xml), the Angular client generated a single method with a union return type but no way to specify which content type to request. Angular's HttpClient requires responseType to be set correctly at request time, causing runtime failures when non-JSON responses were returned.

Additionally, MSW mock imports were incorrectly dropped when text/plain was present among content types.

Solution

Angular Generator (@orval/angular)

  • Generates function overloads for endpoints with multiple content types
  • Each overload accepts an accept parameter typed to a specific content type literal
  • Runtime responseType is dynamically set based on the Accept header:
    • json for JSON content types
    • text for text/* and XML content types
    • blob for binary content types
  • Default content type prioritizes application/json > any +json type > first in list

MSW Mock Generator (@orval/mock)

  • Fixed import filtering to preserve model type imports when text/plain is among content types

Core Utilities (@orval/core)

  • Added getDefaultContentType() utility for consistent default selection across generators

Generated Code Example

// Before: No way to request specific content type
showPetById(petId: string): Observable<string | Pet>

// After: Type-safe overloads with content negotiation
showPetById(petId: string, accept: 'text/plain'): Observable<string>;
showPetById(petId: string, accept: 'application/json'): Observable<Pet>;
showPetById(petId: string, accept?: string): Observable<Pet>; // defaults to JSON

Breaking Changes

None. This is additive - existing code using singular content type endpoints works unchanged.

Future Work

( I can try also to do this if you want )

  • Extend function overloading to Axios generator (@orval/axios)
  • Extend function overloading to Fetch generator (@orval/fetch)
  • Update MSW mock generator to dynamically respond based on Accept header

Testing

  • Angular package builds successfully
  • Sample angular-app regenerated and builds
  • Manual testing confirms correct Accept headers and response parsing

@ScriptType ScriptType marked this pull request as draft December 23, 2025 04:12
@ScriptType ScriptType force-pushed the Different-response-types-causes-issues branch from 65069ba to f750262 Compare December 23, 2025 04:18
@ScriptType ScriptType changed the title Different response types causes issues fix(mock,angular): Different response types causes issues Dec 23, 2025
@melloware
Copy link
Collaborator

PRs for fixing Axios and fetch and others would be awesome!

@ScriptType
Copy link
Contributor Author

ScriptType commented Dec 23, 2025

The thing is i don't know if there are scenarios where it is breaking for fetch/axios.
Angular Http didn't worked with union types so it is pretty safe to do this, but if fetch/axios works but maybe not 100% correct it could break code when it now defaults to json instead for example text like it did before.
Someone needs to look at it and decides how to handle it there.

@melloware
Copy link
Collaborator

No worries this fix is great for Angular.

@melloware melloware force-pushed the Different-response-types-causes-issues branch from f750262 to 270e646 Compare December 23, 2025 12:39
@ScriptType ScriptType marked this pull request as ready for review December 23, 2025 12:57
@ScriptType
Copy link
Contributor Author

@melloware Than i would say i make it open for Review (atleast when this is the route we want to go).

We still need to create Issues for the other generators (if you think that we need it there). :)

@melloware
Copy link
Collaborator

@snebjorn if you want to review

@melloware
Copy link
Collaborator

I am going to merge this so it can be in RC5

@melloware melloware merged commit 833678f into orval-labs:master Dec 23, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Angular: Multiple response types not handled properly when response application/pdf Angular: Different response types causes issues

2 participants