Comments (13)
@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.
Undertsood. PR's are welcome...
from orval.
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.
from orval.
@Will-Mann-16 this looks like an easy one to verify and fix for MSW?
from orval.
I'll take a look.
from orval.
@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?
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.
Will close until we hear back from OP
from orval.
And 6.20.0 just came out just verifying it still happens?
from orval.
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.
@Kcazer is that this issue: #910 ??
from orval.
I don't think it's related, we're using different names and there is no nesting on my latest code sample
from orval.
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.
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)
- Add support to use Biome in addition to Prettier HOT 12
- Zod: Components override not working HOT 1
- [Hono] Generated handler cannot be implemeted (`Argument of type 'string' is not assignable to parameter of type 'never'`) HOT 8
- [Hono] repsonse validator generated when there is no response HOT 1
- Zod: Support for OpenApi spec 3.1.0 HOT 2
- SWR client how to get response headers
- Discriminators are not taken into consideration when inlined in response HOT 3
- React Query: Support passing `query.meta` / `mutation.meta` options to axios instance HOT 1
- Feature request: Split polymorphic mocks into it's own functions
- Build setting allParamsOptional is not working as expected HOT 3
- swr: Mutation fetchers are not strongly typed
- OpenAPI: Support for `const` values in string parameter types HOT 5
- Zod: add to @orval/zod support of anyOf/allOf/oneOf in requestBody
- MSW: overrideResponse can be a function HOT 2
- Feature Request: create factory method for instantiating OpenAPI models HOT 2
- ERR_UNSUPPORTED_ESM_URL_SCHEME when using transformer on Windows HOT 3
- faker override to much for nested object HOT 1
- Query options for an operation have no effect
- RangeError when generating mocks HOT 1
- NonReadonly type incorrectly includes readonly fields HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from orval.