-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
feat: generate const object for const schema definition #1850
base: master
Are you sure you want to change the base?
feat: generate const object for const schema definition #1850
Conversation
340373e
to
c17c462
Compare
@fjodor-rybakov |
I have some api like this: openapi: 3.1.0
paths:
/get-bubble-gums/{id}:
get:
parameters:
- in: path
name: id
schema:
type: number
responses:
'200':
description: Suceess response
content:
application/json:
schema:
#some success schems
'404':
description: Not found
content:
application/json:
schema:
$ref: '#/components/schemas/BubbleGumIdNotFoundException'
components:
schemas:
BubbleGumIdNotFoundException:
type: object
properties:
message:
type: string
const: 'Bubble gum not found'
code:
type: integer
const: 1
description: 'Unique exception code'
required: [ 'message', 'code' ] In specification I write if (response.body.code === BubbleGumIdNotFoundExceptionValue.code) {
// do some
} else {
// do else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good. I double-checked the specifications and understood that const definitions are declarations for managing constants like enums.
- https://json-schema.org/draft/2020-12/json-schema-validation#name-const
- Defining constant value in response OAI/OpenAPI-Specification#1313
- https://swagger.io/specification/
Please comment what you would like to change.
And please also add the following two files.
- add test using
const
todefault.config.ts
https://github.com/orval-labs/orval/blob/master/tests/configs/default.config.ts
- add
useConstForSchemaDefinition
property to guide
https://github.com/orval-labs/orval/blob/master/docs/src/pages/reference/configuration/output.md
9b25fc5
to
9948e5e
Compare
|
@fjodor-rybakov |
Indeed, you are right, this can be made the default behavior without set |
@fjodor-rybakov |
Okey, I am get rid of |
Running build again! |
It turned out that not everything was so simple. |
Well, it seems like you need to change the output as below: - export type StringConstValueNullable = 'string' | null | null;
+ export const StringConstValueNullable = 'string' as const;
+ export type StringConstValueNullableType = typeof StringConstValueNullable | null;
- import type { StringConstValueNullable } from './stringConstValueNullable';
+ import { StringConstValueNullable } from './stringConstValueNullable';
export const StringConstValue = {
value: 'string',
valueWithoutType: 'string',
valueNullable: StringConstValueNullable,
} as const;
export type StringConst = typeof StringConstValue; |
Hmm... I think some wrong in test spec openapi: 3.1.0
info:
title: Const
version: 1.0.0
paths: {}
components:
schemas:
StringConst:
type: object
required:
- value
- valueWithoutType
- valueNullable
properties:
value:
type: string
const: string
valueWithoutType:
const: string
valueNullable:
type:
- string
- 'null'
const: string In my opinion, adding nulls to types is confusing. As if constant could change... |
…require in interface
6313bb0
to
907cd7d
Compare
Status
READY
Description
#1849
Add new
useConstForSchemaDefinition
for generateconst
insteadinterface
for schema definition.For example:
With enabled
useConstForSchemaDefinition
i will get follow generated codeRelated PRs
List related PRs against other branches:
Todos
Steps to Test or Reproduce
Outline the steps to test or reproduce the PR here.