diff --git a/examples/advanced/petStore.yaml b/examples/advanced/petStore.yaml index 47e49ced8..214067980 100644 --- a/examples/advanced/petStore.yaml +++ b/examples/advanced/petStore.yaml @@ -831,9 +831,11 @@ components: type: integer format: int64 example: 10 + readOnly: true name: type: string example: doggie + writeOnly: true category: $ref: "#/components/schemas/Category" photoUrls: diff --git a/examples/advanced/src/gen/models/ts/Pet.ts b/examples/advanced/src/gen/models/ts/Pet.ts index f4915f441..706e31829 100644 --- a/examples/advanced/src/gen/models/ts/Pet.ts +++ b/examples/advanced/src/gen/models/ts/Pet.ts @@ -12,7 +12,7 @@ export type Pet = { * @type integer | undefined int64 * @example 10 */ - id?: number + readonly id?: number /** * @type string * @example doggie diff --git a/examples/advanced/src/gen/zod/petSchema.ts b/examples/advanced/src/gen/zod/petSchema.ts index 2634dc3ee..dc7c4b4ed 100644 --- a/examples/advanced/src/gen/zod/petSchema.ts +++ b/examples/advanced/src/gen/zod/petSchema.ts @@ -4,7 +4,7 @@ import { categorySchema } from './categorySchema' import { tagSchema } from './tagSchema' export const petSchema = z.object({ - id: z.number().optional(), + id: z.number().readonly().optional(), name: z.string(), category: z.lazy(() => categorySchema).optional(), photoUrls: z.array(z.string()), diff --git a/packages/parser/src/codegen.ts b/packages/parser/src/codegen.ts index 9ce15e066..a7e5bfdef 100644 --- a/packages/parser/src/codegen.ts +++ b/packages/parser/src/codegen.ts @@ -112,17 +112,24 @@ export function createUnionDeclaration({ nodes, withParentheses }: { nodes: Arra } export function createPropertySignature({ - modifiers, + readOnly, + modifiers = [], name, questionToken, type, }: { + readOnly?: boolean modifiers?: Array name: ts.PropertyName | string questionToken?: ts.QuestionToken | boolean type?: ts.TypeNode }) { - return factory.createPropertySignature(modifiers, propertyName(name), createQuestionToken(questionToken), type) + return factory.createPropertySignature( + [...modifiers, readOnly ? factory.createToken(ts.SyntaxKind.ReadonlyKeyword) : undefined].filter(Boolean) as Array, + propertyName(name), + createQuestionToken(questionToken), + type, + ) } export function createParameterSignature( diff --git a/packages/swagger-ts/src/generators/TypeGenerator.ts b/packages/swagger-ts/src/generators/TypeGenerator.ts index dd845fa29..0c7e5ca75 100644 --- a/packages/swagger-ts/src/generators/TypeGenerator.ts +++ b/packages/swagger-ts/src/generators/TypeGenerator.ts @@ -141,6 +141,7 @@ export class TypeGenerator extends SchemaGenerator = { max: '.max', optional: '.optional', catchall: '.catchall', + readOnly: '.readonly', // custom ones ref: 'ref', @@ -119,6 +121,7 @@ type ZodMetaEmail = { keyword: typeof zodKeywords.email } type ZodMetaUuid = { keyword: typeof zodKeywords.uuid } type ZodMetaUrl = { keyword: typeof zodKeywords.url } +type ZodMetaReadOnly = { keyword: typeof zodKeywords.readOnly } export type ZodMeta = | ZodMetaAny @@ -149,6 +152,7 @@ export type ZodMeta = | ZodMetaUuid | ZodMetaLiteral | ZodMetaUrl + | ZodMetaReadOnly /** *