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

Strange enum generation while using anyOf in schema #1385

Closed
danyadanch opened this issue May 19, 2024 · 2 comments · Fixed by #1386
Closed

Strange enum generation while using anyOf in schema #1385

danyadanch opened this issue May 19, 2024 · 2 comments · Fixed by #1386
Assignees
Labels
zod Zod related issue

Comments

@danyadanch
Copy link
Contributor

What are the steps to reproduce this issue?

  1. Use this schema:
{
  "openapi": "3.1.0",
  "info": {
    "title": "TEST API",
    "version": "0.0.1",
    "description": ""
  },
  "paths": {
    "/api/sheets/transactions": {
      "put": {
        "operationId": "updateTransaction",
        "summary": "Update",
        "parameters": [],
        "responses": {
          "200": {
            "description": "OK"
          }
        },
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UpdateTransactionSchema"
              }
            }
          },
          "required": true
        }
      }
    }
  },
  "components": {
    "schemas": {
      "UpdateTransactionSchema": {
        "discriminator": {
          "mapping": {
            "EX": "#/components/schemas/UpdateTransactionSchemaBase",
            "IN": "#/components/schemas/UpdateTransactionSchemaBase",
            "TRANSFER": "#/components/schemas/UpdateTransactionTransferSchemaBase"
          },
          "propertyName": "type"
        },
        "oneOf": [
          {
            "$ref": "#/components/schemas/UpdateTransactionSchemaBase"
          },
          {
            "$ref": "#/components/schemas/UpdateTransactionTransferSchemaBase"
          }
        ],
        "title": "UpdateTransactionSchema"
      },
      "UpdateTransactionSchemaBase": {
        "additionalProperties": false,
        "properties": {
          "type": {
            "enum": [
              "IN",
              "EX"
            ],
            "title": "Type",
            "type": "string"
          }
        },
        "required": [
          "type"
        ],
        "title": "UpdateTransactionSchemaBase",
        "type": "object"
      },
      "UpdateTransactionTransferSchemaBase": {
        "additionalProperties": false,
        "properties": {
          "type": {
            "const": "TRANSFER",
            "title": "Type"
          }
        },
        "required": [
          "type"
        ],
        "title": "UpdateTransactionTransferSchemaBase",
        "type": "object"
      }
    }
  },
  "servers": []
}
  1. generate Zod schema

What happens?

/**
 * Generated by orval v6.29.1 🍺
 * Do not edit manually.
 * TEST API
 * OpenAPI spec version: 0.0.1
 */
import {
  z as zod
} from 'zod'


/**
 * @summary Update
 */
export const updateTransactionBody = zod.object({
  "type": zod.enum(['IN', 'EX', 'EX', 'IN'])
}).or(zod.object({
  "type": zod.enum(['TRANSFER'])
}))

What were you expecting to happen?

/**
 * Generated by orval v6.29.1 🍺
 * Do not edit manually.
 * TEST API
 * OpenAPI spec version: 0.0.1
 */
import {
  z as zod
} from 'zod'


/**
 * @summary Update
 */
export const updateTransactionBody = zod.object({
  "type": zod.enum(['EX', 'IN'])
}).or(zod.object({
  "type": zod.enum(['TRANSFER'])
}))

@danyadanch
Copy link
Contributor Author

@melloware to be honest i need some help with this because my pr is just proof of concept that fixes my bug

@melloware
Copy link
Collaborator

i added @anymaniax as a reviewer and @soartec-lab

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 a pull request may close this issue.

2 participants