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

fix: remove readOnly fields from body schema parsing before Zod generation #1625

Conversation

yoshi2no
Copy link
Contributor

Status

READY

Description

close #1548

  • Updated parseBodyAndResponse to apply removeReadOnlyProperties when parsing the body, ensuring that readOnly fields are excluded before generating the Zod schema.
  • This ensures that only non-readOnly fields are considered for the body schema

Related PRs

List related PRs against other branches:

branch PR
other_pr_production link
other_pr_master link

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

Set up the following files and verify that the generated body schema does not include any properties that have readOnly set to true.

orval.config.js
import { defineConfig } from "orval";

export default defineConfig({
  petstore: {
    input: "./petstore.yaml",
    output: {
      client: "zod",
      target: "./src/generated/zod.ts",
      override: {},
    },
  },
});
petstore.yaml
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Simple User API
  license:
    name: MIT
servers:
  - url: http://example.com/v1
paths:
  /users:
    get:
      summary: List all users
      operationId: listUsers
      responses:
        "200":
          description: A list of users
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/User"
    post:
      summary: Create a user
      operationId: createUser
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/User"
      responses:
        "201":
          description: User created successfully
  /users/multiple:
    post:
      summary: Create multiple users
      operationId: createMultipleUsers
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: "#/components/schemas/User"
      responses:
        "201":
          description: Users created successfully

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
          format: uuid
          readOnly: true
        username:
          type: string
          maxLength: 50
        email:
          type: string
          format: email
        created_at:
          type: string
          format: date-time
          readOnly: true
      required:
        - username
        - email

the output in my environment is as follows:

before the change
/**
 * Generated by orval v7.1.0 🍺
 * Do not edit manually.
 * Simple User API
 * OpenAPI spec version: 1.0.0
 */
import { z as zod } from "zod";

/**
 * @summary List all users
 */
export const listUsersResponseUsernameMax = 50;

export const listUsersResponseItem = zod.object({
  id: zod.string().uuid().optional(),
  username: zod.string().max(listUsersResponseUsernameMax),
  email: zod.string().email(),
  created_at: zod.string().datetime().optional(),
});
export const listUsersResponse = zod.array(listUsersResponseItem);

/**
 * @summary Create a user
 */
export const createUserBodyUsernameMax = 50;

export const createUserBody = zod.object({
  id: zod.string().uuid().optional(),
  username: zod.string().max(createUserBodyUsernameMax),
  email: zod.string().email(),
  created_at: zod.string().datetime().optional(),
});

/**
 * @summary Create multiple users
 */
export const createMultipleUsersBodyUsernameMax = 50;

export const createMultipleUsersBodyItem = zod.object({
  id: zod.string().uuid().optional(),
  username: zod.string().max(createMultipleUsersBodyUsernameMax),
  email: zod.string().email(),
  created_at: zod.string().datetime().optional(),
});
export const createMultipleUsersBody = zod.array(createMultipleUsersBodyItem);
after the change
/**
 * Generated by orval v7.1.0 🍺
 * Do not edit manually.
 * Simple User API
 * OpenAPI spec version: 1.0.0
 */
import { z as zod } from "zod";

/**
 * @summary List all users
 */
export const listUsersResponseUsernameMax = 50;

export const listUsersResponseItem = zod.object({
  id: zod.string().uuid().optional(),
  username: zod.string().max(listUsersResponseUsernameMax),
  email: zod.string().email(),
  created_at: zod.string().datetime().optional(),
});
export const listUsersResponse = zod.array(listUsersResponseItem);

/**
 * @summary Create a user
 */
export const createUserBodyUsernameMax = 50;

export const createUserBody = zod.object({
  username: zod.string().max(createUserBodyUsernameMax),
  email: zod.string().email(),
});

/**
 * @summary Create multiple users
 */
export const createMultipleUsersBodyUsernameMax = 50;

export const createMultipleUsersBodyItem = zod.object({
  username: zod.string().max(createMultipleUsersBodyUsernameMax),
  email: zod.string().email(),
});
export const createMultipleUsersBody = zod.array(createMultipleUsersBodyItem);

@melloware melloware added the zod Zod related issue label Sep 14, 2024
@melloware melloware merged commit bdc584b into orval-labs:master Sep 14, 2024
2 checks passed
@JargeZ
Copy link

JargeZ commented Sep 14, 2024

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
zod Zod related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Zod: readonly fields appears in *CreateBody
3 participants