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

Mocks (for MSW) are wrongly generated with deeply nested objects #1692

Open
Hypenate opened this issue Nov 7, 2024 · 3 comments · May be fixed by #1815
Open

Mocks (for MSW) are wrongly generated with deeply nested objects #1692

Hypenate opened this issue Nov 7, 2024 · 3 comments · May be fixed by #1815
Labels
mock Related to mock generation msw MSW related issues
Milestone

Comments

@Hypenate
Copy link
Contributor

Hypenate commented Nov 7, 2024

What are the steps to reproduce this issue?

YAML Path

  '/api/tenants/{tenantId}':
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TenantComposite'

YAML Schema

    TenantComposite:
      allOf:
        - $ref: '#/components/schemas/Tenant'

    TenantComposite:
      allOf:
        - $ref: '#/components/schemas/Tenant'
        
    Tenant:
      allOf:
        - $ref: '#/components/schemas/TenantLight'

What happens?

In the .msw file, it generates the following:
export const getGetApiTenantsTenantIdResponseMock = (): TenantCompositeDto => ({...enabled: faker.datatype.boolean(),

What were you expecting to happen?

In the .msw file, it generated the following:
export const getGetApiTenantsTenantIdResponseMock = (): TenantCompositeDto => ({enabled: faker.datatype.boolean(),

What versions are you using?

7.2.0

@Hypenate
Copy link
Contributor Author

Hypenate commented Nov 7, 2024

Maybe related to #1635

@melloware melloware added bug Something isn't working msw MSW related issues labels Nov 7, 2024
@SHergibo
Copy link

I think I have the same problem since I have updated Orval from version 6.25.0 to version 7.3.0.

I'm using these versions:
Orval: 7.3.0
msw: 2.7.0
faker-js: 8.4.1

YAML reproduction

openapi: '3.0.0'
info:
  version: 1.0.0
  title: example
  license:
    name: MIT
paths:
  /tasks:
    post:
      tags: ['Tasks']
      operationId: createTask
      summary: Create a task
      description: Create a new task.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateTaskRequest'
      responses:
        '201':
          description: The task is created and ready to be used.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TaskTranslated'
          headers:
            Location:
              schema:
                type: string
              description: Relative URL to access this task
components:
  schemas:
    CreateTaskRequest:
      allOf:
        - $ref: '#/components/schemas/Translations'

    Task:
      allOf:
        - $ref: '#/components/schemas/TaskHref'
        - type: object
          properties:
            name:
              type: string
              example: 'Take a walk'
          required:
            - name

    TaskTranslated:
      allOf:
        - $ref: '#/components/schemas/Task'
        - $ref: '#/components/schemas/Translations'

    TaskHref:
      type: object
      required: ['href']
      properties:
        href:
          type: string
          x-data-type: TaskHrefValue
          example: '/tasks/f2584c95-7df7-4f02-8181-8d0f3547cb87'

    Locale:
      type: string
      pattern: '^[a-zA-Z]{2}(-[a-zA-Z]{2})?$'
      default: 'en'
      example: 'en-gb'

    Translation:
      type: object
      properties:
        locale:
          $ref: '#/components/schemas/Locale'
        value:
          type: string
          example: 'Lorem'
      required:
        - locale
        - value

    Translations:
      type: object
      properties:
        translations:
          type: array
          items:
            $ref: '#/components/schemas/Translation'
          minItems: 0
          uniqueItems: true
      required:
        - translations

What happens?

Orval generate this in the .msw file:

export const getCreateTaskResponseMock = (): TaskTranslated => (
  {
    ...href: faker.string.alpha(20),
    name: faker.string.alpha(20),
    translations: Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => (
      {
        locale: faker.helpers.fromRegExp('^[a-zA-Z]{2}(-[a-zA-Z]{2})?$'), 
        value: faker.string.alpha(20)
      }))
  })

Screenshot 2024-12-18 at 11 04 50

What were you expecting to happen?

export const getCreateTaskResponseMock = (): TaskTranslated => (
  {
    href: faker.string.alpha(20),
    name: faker.string.alpha(20),
    translations: Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => (
      {
        locale: faker.helpers.fromRegExp('^[a-zA-Z]{2}(-[a-zA-Z]{2})?$'), 
        value: faker.string.alpha(20)
      }))
  })

PS: I tried to use the Orval playground with the provided YAML reproduction and getCreateTaskResponseMock isn't even properly generated:

export const getCreateTaskResponseMock = (): TaskTranslated => ({});

@melloware melloware added mock Related to mock generation and removed bug Something isn't working labels Dec 18, 2024
@tunganh-phamba
Copy link

confirm I got same Problem in version 7.3.0

@AllieJonsson AllieJonsson linked a pull request Jan 13, 2025 that will close this issue
3 tasks
@melloware melloware added this to the 7.4.2 milestone Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mock Related to mock generation msw MSW related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants