Skip to content

API mock handlers not using overrideResponse parameter when response content-type is text/plain #2115

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

Closed
SampsaKaskela opened this issue May 26, 2025 · 4 comments · Fixed by #2123
Assignees
Labels
mock Related to mock generation
Milestone

Comments

@SampsaKaskela
Copy link

SampsaKaskela commented May 26, 2025

What are the steps to reproduce this issue?

I'm generating using following yaml

openapi: '3.0.2'
info:
  title: API
  version: '1.0'
  
paths:
  /{param1}:
    get:
      parameters:
        - in: path
          name: param1
          schema:
            type: string
          required: true
      responses:
        '200':
          description: OK
          content:
            text/plain:
              schema:
                type: string

This is my Orval config

import { defineConfig } from 'orval';

export default defineConfig({
  test: {
    output: {
      workspace: `src/generated/api/test`,
      target: './api.ts',
      mock: true,
    },
    input: {
      target: `./src/generated/yaml/test.yaml`,
    },
  },
});

What happens?

I get following function that doesn't utilize overrideResponse in any way.

export const getGetParam1MockHandler = (overrideResponse?: string | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<string> | string)) => {
  return http.get('*/:param1', async (info) => {await delay(1000);
  
    return new HttpResponse(getGetParam1ResponseMock(),
      { status: 200,
        headers: { 'Content-Type': 'text/plain' }
      })
  })
}

What were you expecting to happen?

overrideResponse variable should be used. If I switch to application/json type then I get the following which looks correct.

export const getGetParam1MockHandler = (overrideResponse?: string | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<string> | string)) => {
  return http.get('*/:param1', async (info) => {await delay(1000);
  
    return new HttpResponse(JSON.stringify(overrideResponse !== undefined 
            ? (typeof overrideResponse === "function" ? await overrideResponse(info) : overrideResponse) 
            : getGetParam1ResponseMock()),
      { status: 200,
        headers: { 'Content-Type': 'application/json' }
      })
  })
}

Any logs, error output, etc?

Just following, no errors.

🍻 Start orval v7.9.0 - A swagger client generator for typescript
🎉 test - Your OpenAPI spec has been converted into ready to use orval!

What versions are you using?

Orval 7.9.0

@melloware melloware added the mock Related to mock generation label May 26, 2025
@KyleNeedham
Copy link
Contributor

I noticed this logic while reviewing a different fix last week. I’m not sure why the override response isn’t used for text/plain responses—there doesn’t seem to be a clear reason not to apply overrides to all response types.

Maybe @melloware can shed some light on this?

? isTextPlain
? `${getResponseMockFunctionName}()`
: `JSON.stringify(overrideResponse !== undefined
? (typeof overrideResponse === "function" ? await overrideResponse(${infoParam}) : overrideResponse)
: ${getResponseMockFunctionName}())`

@melloware
Copy link
Collaborator

No idea maybe look at Git Blame and see who made that change and for what ticket and why?

@KyleNeedham
Copy link
Contributor

PR submitted. Based on the history it was originally to avoid calling JSON.stringify on string response types.

@melloware melloware added this to the 7.10.0 milestone May 28, 2025
@melloware
Copy link
Collaborator

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mock Related to mock generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants