Skip to content

Add a flag to skip Schema prefix when rendering root types #1958

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

Merged
merged 7 commits into from
Nov 27, 2024

Conversation

sultaniman
Copy link
Contributor

@sultaniman sultaniman commented Oct 23, 2024

Changes

This PR adds another cli flag --root-types-no-prefix which complements --root-types.
If set it will render root types without Schema prefix.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

Copy link

changeset-bot bot commented Oct 23, 2024

⚠️ No Changeset found

Latest commit: fb59371

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@sultaniman sultaniman force-pushed the feat/root-types-no-prefix branch 3 times, most recently from dd55bfe to 2a92289 Compare October 23, 2024 13:54
@sultaniman sultaniman force-pushed the feat/root-types-no-prefix branch from 2a92289 to 3fa8dbe Compare October 23, 2024 13:56
Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In favor of this in general! I think we need some tests here just to ensure it works as expected, but that test can be minimal since it’s an opt-in flag.

Also agree with the “off/on” approach here, since the prefixes MAY be Schema but also may be Response or Request, etc. I don’t think it would make sense to have a flag to manually set that prefix, since that prefix isn’t hardcoded to begin with.

But with that in mind, a test that **tests what happens when you have a naming conflict between #/components/schemas and #/components/responses would be great (not required, but it would really help validate that all bases are being covered)

@drwpow drwpow added the openapi-ts Relevant to the openapi-typescript library label Oct 26, 2024
@sultaniman
Copy link
Contributor Author

Hey @drwpow thanks for review, I will add tests and update the PR with requested changes some time next week.

@sultaniman sultaniman marked this pull request as ready for review November 5, 2024 13:14
@sultaniman sultaniman requested a review from a team as a code owner November 5, 2024 13:14
@sultaniman
Copy link
Contributor Author

@drwpow I will add another test which tests when there is a parameter with the same name. I added a test case for the new flag.

@sultaniman
Copy link
Contributor Author

@drwpow can you please take a look again?

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. This looks FANTASTIC. Thank you!

@drwpow drwpow merged commit 3f43c45 into openapi-ts:main Nov 27, 2024
8 checks passed
@sultaniman sultaniman deleted the feat/root-types-no-prefix branch November 28, 2024 06:28
@sultaniman
Copy link
Contributor Author

That's awesome, thanks @drwpow!

DjordyKoert pushed a commit to DjordyKoert/openapi-typescript that referenced this pull request Nov 28, 2024
…s#1958)

* Add a flag to skip Schema prefix when rendering root types

* Add missing option in tests

* Add test case

* Fix linting issues

* Update examples

* Update docs

* Update examples
@fluctus
Copy link

fluctus commented Jan 1, 2025

@sultaniman @drwpow First, great job on adding the new --root-types-no-prefix flag! It’s a really useful addition.

However, I've noticed that when the --root-types-no-prefix flag is used alongside the --enum flag, it generates a root type with the same name as the enum, leading to naming conflicts.

Given that enums already serve as types in TypeScript, it seems unnecessary to generate a separate root type in these cases. Skipping the root type when both flags are used would help avoid conflicts and streamline the output.

It would be great if this could be addressed, whether by skipping the root type or considering other potential solutions to prevent such conflicts. Looking forward to seeing how this can be improved!

@sultaniman
Copy link
Contributor Author

Hey @fluctus @drwpow I think I can adjust with additional check if --enum flag is passed then maybe skipping prefix trimming, but probably we first need to discuss the desired behavior.

@fluctus
Copy link

fluctus commented Jan 2, 2025

👋 Good point @sultaniman. Regarding the desired behavior, I think it would be a bit odd if this feature doesn't work when the --enum flag is used. A better solution might be to make both flags work together without conflicts.

One option could be to skip generating the root type only for enums when both the --root-types-no-prefix and --enum flags are used. This would avoid naming conflicts while keeping the intended behavior intact. If there's another way to handle both flags together without issues, that would work too.

@drwpow
Copy link
Contributor

drwpow commented Jan 3, 2025

Yeah with some of these flags, they’re not always perfectly compatible with one another. We could either throw an error that these flags don’t work together. Or do what @fluctus suggested by modifying the behavior of --root-types-no-prefix. As long as we DON’T modify the behavior of the --enum flag in any way I think that’s a fine solution.

@sultaniman
Copy link
Contributor Author

alright then as @fluctus suggested it would mean if --enum is passed then we skip --root-types only for enums?

@fluctus
Copy link

fluctus commented Jan 19, 2025

@sultaniman if @drwpow agrees with this solution, would you be handling the fix?

@sultaniman
Copy link
Contributor Author

Hey @fluctus sorry for being slow, I was sick, will try to carve out some time this week for it.

@fluctus
Copy link

fluctus commented Jan 20, 2025

@sultaniman No worries, hope you're feeling better! Thanks for taking the time to look into this.

@sultaniman
Copy link
Contributor Author

@fluctus can you please provide some minimal OpenAPI spec example with enum definitions you have?

@fluctus
Copy link

fluctus commented Jan 26, 2025

@sultaniman Sure, this is a sample spec that generates types with the same name for enums:

openapi: 3.0.3
info:
  title: My API
  version: 1.0.0
components:
  schemas:
    NonEnumSchema:
      type: string
    EnumSchema:
      type: string
      enum:
        - VALUE1
        - VALUE2

Without --root-types-no-prefix:

export type paths = Record<string, never>;
export type webhooks = Record<string, never>;
export interface components {
    schemas: {
        NonEnumSchema: string;
        /** @enum {string} */
        EnumSchema: EnumSchema;
    };
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: never;
}
export type SchemaNonEnumSchema = components['schemas']['NonEnumSchema'];
export type SchemaEnumSchema = components['schemas']['EnumSchema'];
export type $defs = Record<string, never>;
export enum EnumSchema {
    VALUE1 = "VALUE1",
    VALUE2 = "VALUE2"
}
export type operations = Record<string, never>;

With --root-types-no-prefix:

export type paths = Record<string, never>;
export type webhooks = Record<string, never>;
export interface components {
    schemas: {
        NonEnumSchema: string;
        /** @enum {string} */
        EnumSchema: EnumSchema;
    };
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: never;
}
export type NonEnumSchema = components['schemas']['NonEnumSchema'];
export type EnumSchema = components['schemas']['EnumSchema'];
export type $defs = Record<string, never>;
export enum EnumSchema {
    VALUE1 = "VALUE1",
    VALUE2 = "VALUE2"
}
export type operations = Record<string, never>;

Which results in these TS errors:
TS2502: EnumSchema is referenced directly or indirectly in its own type annotation.
TS2456: Type alias EnumSchema circularly references itself.
TS2567: Enum declarations can only merge with namespace or other enum declarations.

@sultaniman
Copy link
Contributor Author

@fluctus thank you very much for providing an example I will use it in the tests.

@theo-staizen
Copy link

theo-staizen commented Feb 12, 2025

Hi @sultaniman @fluctus I have also run into exactly those 3 errors, because I was running both --root-types-no-prefix and --enums. Is there no way to fix this and keep both flags?

@fluctus
Copy link

fluctus commented Feb 13, 2025

@theo-staizen yes, @sultaniman is working on a fix that will work with both flags.

@sultaniman
Copy link
Contributor Author

Hey guys, sorry for delays I wanted to work on this last week but was unable to do so. I will do it in the coming weekend and the following week.

@theo-staizen
Copy link

hi @sultaniman, sorry to bother you, but have you had a chance to work on this at all? is there anything we can do to help?

@sultaniman
Copy link
Contributor Author

Good evening @drwpow @theo-staizen @fluctus sorry for being so slow had many personal things going on recently. I linked the PR which disables --root-types-no-schema-prefix if used with --enum flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants