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

oas2ts generates duplicate type declaration #177

Closed
jmjf opened this issue Feb 16, 2024 · 5 comments
Closed

oas2ts generates duplicate type declaration #177

jmjf opened this issue Feb 16, 2024 · 5 comments
Assignees

Comments

@jmjf
Copy link
Contributor

jmjf commented Feb 16, 2024

Given the OpenAPI YAML below, openapi-transformer-toolkit oas2ts imports the ABParams type and generates type declaration for it in Path1Query.d.ts and Path2Query.d.ts. The duplicate declaration breaks lint rules against redeclaring types.

Any ideas what might be causing this issue, why it things it needs to import and redeclare the type, or by what rationale importing and redeclaring the type is wise? (And if it is wise, how do I inject a lint ignore in the generated type without manually editing every time I generate?)

This example is a small reproduction. The real world case is a pair of routes that give different views of the same data. The routes require either AParams or BParams or both (required enforced in business logic) and also accept CommonOpts (filter criteria). Path2 also accepts ExtraOpts (sorting, pagination, etc.).

openapi: 3.1.0
info:
  title: Test API
  description: Testing complex query parameters
  version: 0.0.1
  license: 
    name: MIT
    url: https://nobody.org

servers:
  - url: http://127.0.0.0:3000

paths:
  /path1:
    get:
      operationId: Path1
      security: []
      summary: Path1 endpoint for testing query strings
      parameters:
        - in: query
          name: path1Query
          schema:
            $ref: '#/components/schemas/Path1Query'
      responses:
        '200':
          description: result
          content:
            application/json:
              schema:
                type: object
                properties:
                  reqId:
                    type: string
                  query:
                    type: object
                    additionalProperties: true
        '4xx':
            description: error
  /path2:
    get:
      operationId: Path2
      security: []
      summary: Path2 endpoint for testing query strings
      parameters:
        - in: query
          name: path2Query
          schema:
            $ref: '#/components/schemas/Path2Query'
      responses:
        '200':
          description: result
          content:
            application/json:
              schema:
                type: object
                properties:
                  reqId:
                    type: string
                  query:
                    type: object
                    additionalProperties: true
        '4xx':
            description: error
components:
  schemas:
    AParams:
      type: object
      properties:
        a1:
          type: string
        a2:
          type: string
      required:
        - a1
        - a2
    BParams:
      type: object
      properties:
        b1:
          type: string
        b2:
          type: string
      required:
        - b1
        - b2
    ABParams:
      type: object
      allOf:
        - $ref: '#/components/schemas/AParams'
        - $ref: '#/components/schemas/BParams'
    CommonOpts:
      type: object
      properties:
        opt1:
          type: string
        opt2:
          type: string
    ExtraOpts:
      type: object
      properties:
        extra1:
          type: string
        extra2:
          type: string
    Path1Query:
      type: object
      allOf:
        - oneOf:
          - $ref: '#/components/schemas/AParams'
          - $ref: '#/components/schemas/BParams'
          - $ref: '#/components/schemas/ABParams'
        - $ref: '#/components/schemas/CommonOpts'
    Path2Query:
      type: object
      allOf:
        - oneOf:
          - $ref: '#/components/schemas/AParams'
          - $ref: '#/components/schemas/BParams'
          - $ref: '#/components/schemas/ABParams'
        - $ref: '#/components/schemas/CommonOpts'
        - $ref: '#/components/schemas/ExtraOpts'

Offending output (with callout comments added) for Path1Query.d.ts.

import { AParams } from './AParams'
import { BParams } from './BParams'
import { ABParams } from './ABParams'   // <--- ABParams imported
import { CommonOpts } from './CommonOpts'

/* eslint-disable */
/**
 * This file was automatically generated by openapi-transformer-toolkit CLI/methods.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source OpenAPI file,
 * and run openapi-transformer-toolkit CLI/methods to regenerate this file.
 */

export type Path1Query = (AParams | BParams | ABParams) & CommonOpts;
export type ABParams = AParams & BParams;  // <--- ABParams redeclared

Not sure if it helps understand why this is happening, but in the real world case, ABParams has a description and the description comment appears above the redeclaration.

Thanks for any insight or solutions.

PS: If you're wondering, "Why not just use ABParams?" the answer is, docs (generated with @redocly/cli). In the real world case I have titles on the AParams, BParams, and ABParams so API consumers can switch between param sets and see what's required for each.

@pmusce pmusce self-assigned this Feb 16, 2024
@pmusce
Copy link

pmusce commented Feb 19, 2024

From what I see the issue seems to be in json-schema-to-typescript.

Apparently the declareExternallyReferenced flag is not respected for non-interface types
bcherny/json-schema-to-typescript#525

@simoneb
Copy link
Member

simoneb commented Feb 19, 2024

Thanks for looking into this @pmusce . @jmjf @pmusce would any of you be interested in sending a PR to that repo?

@pmusce
Copy link

pmusce commented Feb 19, 2024

@simoneb I will try to send a PR to it

@pmusce
Copy link

pmusce commented Feb 19, 2024

There is already an open PR with the fix same I was going to make, but apparently there are some edge cases that would require quite few changes to it. I am closing this issue since it will be no longer occur on here once the bug in json-schema-to-typescript package is fixed.

@pmusce pmusce closed this as completed Feb 19, 2024
@jmjf
Copy link
Contributor Author

jmjf commented Feb 21, 2024

@pmusce Thanks for diagnosing this. I'll look at the PR and issue there and see if I can figure out a solution. Meanwhile, I've written a simple script to patch the files affected by it.

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

No branches or pull requests

3 participants