hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
Other
1.02k stars 85 forks source link

Unexpected index signature collision #985

Open JoelVenable opened 3 weeks ago

JoelVenable commented 3 weeks ago

Description

Hello, I'm greenfielding a new project and am having typescript compiler issues caused by the index signature.

Frontend application is a freshly bootstrapped Vite app, backend generates the openapi spec using @fastify/swagger.

Screenshot 2024-08-27 at 11 50 17 AM

I understand why the index signature is there, however in our case the client and server are private and we use a code-first approach so incomplete docs should not be a problem for the consumer, therefore we have no need of it in this project.

While the issue needs a fix regardless, is there a way to disable adding the index signatures?

export type CreateTicketData = {
    /**
     * CreateTicketRequest
     */
    body: {
        statusUrl: string;
        vehicle: {
            licensePlate: string;
            licensePlateState: string;
        };
        /**
         * Epoch timestamp, in seconds
         */
        reportedAt: number;
        [key: string]: {
            [key: string]: unknown;
        };
        address: {
            street: string;
            street2?: string;
            city: string;
            state: string;
            zip: string;
        };
        location: {
            lat: number;
            lon: number;
        };
        price: {
            base: number;
            addons: Array<{
                name: string;
                amount: number;
            }>;
            escalationSurcharge: number;
            currency: string;
        };
    };
};

tsconfig.app.json

Reproducible example or configuration

# ./scripts/openapi.ts

import { config } from 'dotenv'
config()
import { createClient } from '@hey-api/openapi-ts'
import axios from 'axios'

async function main() {
  const { API_URL, API_KEY, DEBUG } = process.env
  if (!API_URL) {
    throw new Error('API_URL is required')
  }
  if (!API_KEY) {
    throw new Error('API_KEY is required')
  }
  const docsPath = `${API_URL}/api/__docs`
  const schema = await axios
    .get(docsPath, {
      headers: { 'x-api-key': API_KEY, Accept: 'application/json' },
    })
    .then((res) => res.data)

  if (DEBUG) {
    console.log(JSON.stringify(schema, null, 2))
  }
  createClient({
    client: '@hey-api/client-axios',
    input: schema,
    output: 'src/@sdk',
  })
}

main()

OpenAPI specification (optional)

schema.json

System information (optional)

No response

mrlubos commented 2 weeks ago

Hey @JoelVenable, this looks like a legitimate bug where additionalProperties: false should not be producing any index signatures. Once that is fixed, would you still need a way to disable index signatures for other interfaces? The spec you provided is very clean and explicitly disallows additional properties, so combined with the fix that would take care of that problem