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

Generate types for unused schema components #325

Open
BerkliumBirb opened this issue Nov 13, 2022 · 4 comments
Open

Generate types for unused schema components #325

BerkliumBirb opened this issue Nov 13, 2022 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@BerkliumBirb
Copy link

Sometimes backend need to pass object that is not referenced explicitly in API or is wrapped in JWS. In such cases backend developers just expose schemas for such types to conduct a contract. NestJS for example have special extra models configuration property.

Oazapfts only generates types for objects used explicitly and all extra objects are silently dropped from generated code. This behavior is not desirable in many cases.

@Xiphe
Copy link
Collaborator

Xiphe commented Nov 14, 2022

I think it would not hurt to just expose all schemas regardless of if they're used within the methods or not 👍

@Xiphe Xiphe added enhancement New feature or request help wanted Extra attention is needed labels Nov 14, 2022
@bonsmik
Copy link

bonsmik commented Mar 2, 2023

Not enough familiar with the code generation but something like this in generate.ts would do the trick:


   if ( this.spec.components && this.spec.components.schemas)  {
      Object.keys(this.spec.components.schemas).forEach((obj) => {
        const alias = this.aliases.find(a=>a.name.text === obj)
        if ( alias === undefined) {
          const oo = (this.spec.components?.schemas as any)[obj];
          const schema = this.resolve<SchemaObject>(oo);
          const type = this.getTypeFromSchema(schema);
          const name = obj
          const identifier = toIdentifier(name, true);
          const alias = this.getUniqueAlias(identifier);
          this.aliases.push(
            cg.createTypeAliasDeclaration({
              modifiers: [cg.modifier.export],
              name: alias,
              type,
            })
          );
        }
      })
    }

Tested with simple test and not sure if this would be feasible to be used in more advanced scenarios

@Xiphe
Copy link
Collaborator

Xiphe commented Mar 15, 2023

Currently don't have the time to proactively add this. PR's are very welcome!

@trim21
Copy link
Contributor

trim21 commented Mar 19, 2023

Currently we are also using https://www.npmjs.com/package/openapi-typescript to generate an extra copy of types as a workaround.

types are not bundled and don't affect perf,it's much simpler to just generate them twice for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants