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

Zod: add to @orval/zod support of anyOf/allOf/oneOf in requestBody #1327

Open
danyadanch opened this issue Apr 23, 2024 · 0 comments
Open

Zod: add to @orval/zod support of anyOf/allOf/oneOf in requestBody #1327

danyadanch opened this issue Apr 23, 2024 · 0 comments
Labels
zod Zod related issue

Comments

@danyadanch
Copy link
Contributor

What are the steps to reproduce this issue?

  1. Use schema like this:
{
    "openapi": "3.1.0",
    "info": {
        "title": "Luca API",
        "version": "0.0.1",
        "description": ""
    },
    "paths": {
        "/api/sheets/transactions/": {
            "post": {
                "operationId": "createTransaction",
                "summary": "Create",
                "parameters": [],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                },
                "tags": [
                    "sheets/transactions"
                ],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "anyOf": [
                                    {
                                        "$ref": "#/components/schemas/CreateTransactionSchema"
                                    },
                                    {
                                        "$ref": "#/components/schemas/CreateTransactionTransferSchema"
                                    }
                                ],
                                "title": "Schema"
                            }
                        }
                    },
                    "required": true
                }
            }
        }
    },
    "components": {
        "schemas": {
            "CreateTransactionSchema": {
                "properties": {
                    "type": {
                        "$ref": "#/components/schemas/TransactionType"
                    },
                    "amount": {
                        "anyOf": [
                            {
                                "type": "number"
                            },
                            {
                                "type": "string"
                            }
                        ],
                        "title": "Amount"
                    },
                    "account_id": {
                        "format": "uuid",
                        "title": "Account Id",
                        "type": "string"
                    },
                    "accounting_item": {
                        "anyOf": [
                            {
                                "format": "uuid",
                                "type": "string"
                            },
                            {
                                "type": "null"
                            }
                        ],
                        "title": "Accounting Item"
                    },
                    "counterparty": {
                        "anyOf": [
                            {
                                "format": "uuid",
                                "type": "string"
                            },
                            {
                                "type": "null"
                            }
                        ],
                        "title": "Counterparty"
                    },
                    "commentary": {
                        "anyOf": [
                            {
                                "type": "string"
                            },
                            {
                                "type": "null"
                            }
                        ],
                        "default": "",
                        "title": "Commentary"
                    },
                    "payment_date": {
                        "format": "date",
                        "title": "Payment Date",
                        "type": "string"
                    },
                    "accrual_date": {
                        "format": "date",
                        "title": "Accrual Date",
                        "type": "string"
                    }
                },
                "required": [
                    "type",
                    "amount",
                    "account_id",
                    "payment_date",
                    "accrual_date"
                ],
                "title": "CreateTransactionSchema",
                "type": "object"
            },
            "CreateTransactionTransferSchema": {
                "properties": {
                    "type": {
                        "$ref": "#/components/schemas/TransactionType"
                    },
                    "amount": {
                        "anyOf": [
                            {
                                "type": "number"
                            },
                            {
                                "type": "string"
                            }
                        ],
                        "title": "Amount"
                    },
                    "account_id": {
                        "format": "uuid",
                        "title": "Account Id",
                        "type": "string"
                    },
                    "commentary": {
                        "anyOf": [
                            {
                                "type": "string"
                            },
                            {
                                "type": "null"
                            }
                        ],
                        "default": "",
                        "title": "Commentary"
                    },
                    "payment_date": {
                        "format": "date",
                        "title": "Payment Date",
                        "type": "string"
                    },
                    "accrual_date": {
                        "format": "date",
                        "title": "Accrual Date",
                        "type": "string"
                    },
                    "target_amount": {
                        "anyOf": [
                            {
                                "type": "number"
                            },
                            {
                                "type": "string"
                            }
                        ],
                        "title": "Target Amount"
                    },
                    "target_account_id": {
                        "format": "uuid",
                        "title": "Target Account Id",
                        "type": "string"
                    },
                    "target_payment_date": {
                        "format": "date",
                        "title": "Target Payment Date",
                        "type": "string"
                    },
                    "target_accrual_date": {
                        "format": "date",
                        "title": "Target Accrual Date",
                        "type": "string"
                    }
                },
                "required": [
                    "type",
                    "amount",
                    "account_id",
                    "payment_date",
                    "accrual_date",
                    "target_amount",
                    "target_account_id",
                    "target_payment_date",
                    "target_accrual_date"
                ],
                "title": "CreateTransactionTransferSchema",
                "type": "object"
            },
            "TransactionType": {
                "enum": [
                    "IN",
                    "EX",
                    "TRANSFER"
                ],
                "title": "TransactionType",
                "type": "string"
            }
        }
    },
    "servers": []
}
  1. Config like this:
import { defineConfig } from 'orval';

export default defineConfig({
  basic: {
    output: {
      target: '../generated/zod',
      client: 'zod',
      clean: true,
    },
    input: {
      target: 'zod-anyof.json',
    },
  },
}); 

What happens?

I got empty zod file

What were you expecting to happen?

Zod file generated with optional two types

What versions are you using?

Package Version: 6.27.1

@melloware melloware added enhancement New feature or request zod Zod related issue and removed enhancement New feature or request labels Apr 23, 2024
@melloware melloware changed the title feature request: add to @orval/zod support of anyOf/allOf/oneOf in requestBody Zod: add to @orval/zod support of anyOf/allOf/oneOf in requestBody Apr 25, 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

No branches or pull requests

2 participants