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

Duplicate types in nested schemas #646

Open
drewcorlin1 opened this issue Dec 2, 2024 · 0 comments
Open

Duplicate types in nested schemas #646

drewcorlin1 opened this issue Dec 2, 2024 · 0 comments

Comments

@drewcorlin1
Copy link

drewcorlin1 commented Dec 2, 2024

Given a JsonSchema with nested type with a oneOf where the eligible sub-types map to the same typescript type, the resulting typescript type has a duplicate type (eg string | string).

For example

import { compile } from 'json-schema-to-typescript';

compile(
  {
    type: 'object',
    properties: {
      items: {
        type: 'array',
        items: {
          type: 'object',
          properties: {
            myField: { oneOf: [{ type: 'string' }, { type: 'string' }] },
          },
        },
      },
    },
  },
  'MyType',
).then(console.log);

produces

export interface Drew {
  items?: {
    myField?: string | string; // Would have expected just string here
    [k: string]: unknown;
  }[];
  [k: string]: unknown;
}

Interestingly the same does not happen when the oneOf type is at the top level of the JsonSchema

import { compile } from 'json-schema-to-typescript';

compile(
  {
    type: 'object',
    properties: {
      myField: {
        oneOf: [{ type: 'string' }, { type: 'string' }],
      },
    },
  },
  'MyType',
).then(console.log);

produces

export interface MyType {
  myField?: string;
  [k: string]: unknown;
}

In reality my schema uses different formats though they map to the same type, so I simplified the examples above

{
    type: 'object',
    properties: {
      myField: {
        oneOf: [
          { type: 'string', format: 'uuid', },
          { type: 'string', pattern: '^[23456789ABCDEFGHJKMNPQRSTUVWXYZ]{12}$', },
        ],
      },
    },
  }
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

1 participant