Code Monkey home page Code Monkey logo

Comments (13)

Kcazer avatar Kcazer commented on May 24, 2024 1

@melloware @Will-Mann-16 Thank you both for taking time to investigate the issue

I'm working on the same project as @ClementLevesque and have created a repository with a minimal reproduction. You can find it there https://github.com/Kcazer/stunning-winner

I also tried your schema with our config, but, as expected, it was working fine, but I noticed two main differences :

  • Your schema is an OpenApi3, while ours is Swagger2
  • The code generated from your schema uses the generated enum in the mock, but for us, the values are hardcoded (Object.values(GeneratedEnum) vs ['XXX', 'YYY', 'ZZZ'])

from orval.

melloware avatar melloware commented on May 24, 2024 1

Undertsood. PR's are welcome...

from orval.

Will-Mann-16 avatar Will-Mann-16 commented on May 24, 2024 1

PR is up for approval now. You were entirely correct, it was because it wasn't referenced externally. Adding an as const did the trick here.
CleanShot 2023-11-10 at 23 20 01@2x

from orval.

melloware avatar melloware commented on May 24, 2024

@Will-Mann-16 this looks like an easy one to verify and fix for MSW?

from orval.

Will-Mann-16 avatar Will-Mann-16 commented on May 24, 2024

I'll take a look.

from orval.

Will-Mann-16 avatar Will-Mann-16 commented on May 24, 2024

@ClementLevesque looking at this it seems to work correctly, I'm getting a union type passed through correctly and it uses Object.values from the get go. This is the result I get and the openapi config I use. Can you provide a spec where it doesn't work as expected?

CleanShot 2023-11-07 at 11 33 23@2x
openapi: 3.0.0
info:
  title: ""
  description: Generated by astra
  contact: {}
  license:
    name: ""
  version: ""
servers:
  - url: http://localhost:8000/
paths:
  /json:
    get:
      description: GetJSON is a test function
      parameters:
        - name: hello
          in: header
          schema:
            type: string
      responses:
        "200":
          description: ""
          headers:
            x-google-api-key:
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/main.ResponseStruct"
        "400":
          description: ""
          headers:
            x-google-api-key:
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/example_route_in.InResponseStruct"
components:
  schemas:
    database_sql.NullString:
      type: object
      properties:
        String:
          type: string
        Valid:
          type: boolean
      description: |-
        NullString represents a string that may be null.
        NullString implements the Scanner interface so
        it can be used as a scan destination:

        	var s NullString
        	err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
        	...
        	if s.Valid {
        	   // use s.String
        	} else {
        	   // NULL value
        	}
    example_route_in.InResponseStruct:
      type: object
      properties:
        hello:
          type: string
      description: InResponseStruct is a test struct
    main.ResponseStruct:
      type: object
      properties:
        enum-int:
          $ref: "#/components/schemas/main.TestEnumInt"
        enum-string:
          $ref: "#/components/schemas/main.TestEnumString"
        map:
          type: object
          additionalProperties:
            type: string
        not-required:
          type: integer
          format: int32
        required:
          type: string
        separate:
          type: array
          items:
            $ref: "#/components/schemas/example_route_in.InResponseStruct"
        test:
          $ref: "#/components/schemas/database_sql.NullString"
        time:
          $ref: "#/components/schemas/time.Time"
      description: ResponseStruct is a test struct
    main.TestEnumInt:
      enum:
        - 1
        - 2
        - 3
      type: integer
      format: int32
    main.TestEnumString:
      enum:
        - a
        - b
        - c
        - x
        - "y"
        - z
      type: string
    time.Time:
      type: string
      format: date-time

Notice the TestEnumString was the type I was looking at here.

EDIT: So upon further investigation this works as expected when we have an enum as an object's property, but not when an enum is the top level return type. I'll see if I can find a fix for this.

from orval.

melloware avatar melloware commented on May 24, 2024

Will close until we hear back from OP

from orval.

melloware avatar melloware commented on May 24, 2024

And 6.20.0 just came out just verifying it still happens?

from orval.

Kcazer avatar Kcazer commented on May 24, 2024

Still happening with 6.20

But in the meantime, I've managed to track the main cause. It's definitely related to the use of $refs in the source schema. It seems to me than only "global" enums are imported in mock files, not the inline ones.

When using the following schema :

{
  "swagger": "2.0",
  "info": { "title": "orval-repro", "version": "1.0" },
  "definitions": {
    "y": {
      "type": "string",
      "enum": ["AAA", "BBB", "CCC"]
    }
  },
  "paths": {
    "/json": {
      "get": {
        "operationId": "JsonSample",
        "summary": "Used for issue reproduction",
        "tags": [],
        "parameters": [],
        "responses": {
          "200": {
            "description": "Simple JSON Object",
            "schema": {
              "type": "object",
              "properties": {
                "x": {
                  "type": "string",
                  "enum": ["AAA", "BBB", "CCC"]
                },
                "y": {
                  "$ref": "#/definitions/y"
                }
              }
            }
          }
        }
      }
    }
  },
  "basePath": "/",
  "consumes": ["application/json"],
  "produces": ["application/json"]
}

The generated mock will look like this

// Schema
export type JsonSample200X = (typeof JsonSample200X)[keyof typeof JsonSample200X];
export const JsonSample200X = { AAA: "AAA", BBB: "BBB", CCC: "CCC" } as const;

export type Y = (typeof Y)[keyof typeof Y];
export const Y = { AAA: "AAA", BBB: "BBB", CCC: "CCC" } as const;

export type JsonSample200 = { x?: JsonSample200X; y?: Y; };


// Mock
import { faker } from "@faker-js/faker";
import { HttpResponse, delay, http } from "msw";
import { Y } from "./generated.schemas";

export const getJsonSampleMock = () => ({
  // Inline schema => inline values, when `Object.values(JsonSample200X)` would work
  x: faker.helpers.arrayElement([
    faker.helpers.arrayElement(["AAA", "BBB", "CCC"]),
    undefined,
  ]),
  // Reference => is imported, everything works fine
  y: faker.helpers.arrayElement([
    faker.helpers.arrayElement(Object.values(Y)),
    undefined,
  ]),
});

from orval.

melloware avatar melloware commented on May 24, 2024

@Kcazer is that this issue: #910 ??

from orval.

Kcazer avatar Kcazer commented on May 24, 2024

I don't think it's related, we're using different names and there is no nesting on my latest code sample

from orval.

melloware avatar melloware commented on May 24, 2024

Yeah but its basically pointing to a $ref his example talks about nesting but the real issue is it points to a $ref right?

from orval.

Kcazer avatar Kcazer commented on May 24, 2024

Our issue is specifically about the case when no $ref are used. When using refs, mocks are generated with the correct typings, but when inlining enums, they become string instead of being an union of values

from orval.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.