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

Cannot generate component schema refs without use classes #14707

Closed
1 task done
leandroluk opened this issue Feb 26, 2025 · 1 comment
Closed
1 task done

Cannot generate component schema refs without use classes #14707

leandroluk opened this issue Feb 26, 2025 · 1 comment
Labels
needs triage This issue has not been looked into type: enhancement 🐺

Comments

@leandroluk
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I'm having a code to generate swagger schema from Joi.schema (it works and I need to generate from them because this Joi.schema is generated from a minimal and base schema) like this:

import Joi from 'joi';
import {OpenAPIV3} from 'openapi-types';

export type IObjectSchema<T extends {}> = OpenAPIV3.SchemaObject & {
  type: 'object';
  required: Array<IObjectSchema.RequiredKeys<T>>;
  properties: Record<keyof T, OpenAPIV3.SchemaObject>;
};
export namespace IObjectSchema {
  export type RequiredKeys<T> = keyof {
    [K in keyof T as string extends K ? never : {} extends Pick<T, K> ? never : K]: 0;
  };
}

const entitySchema = Joi.object({
  foo: Joi.string(),
  bar: Joi.number(),
  bin: Joi.boolean(),
  test: Joi.date()
})

function makeSwagger<T =any>(schema: Joi.ObjectSchema<T>): IObjectSchema<T> { /* ... */ }

@Controller()
class EntityController{
  @ApiBody({ schema: makeSwagger(entitySchema) })
  get(){}
}

Unfortunately, this type of declaration does not generate a reference in the schemas at the end of the swagger.

Describe the solution you'd like

I would like it to be possible to also pass the name of the type from this type of reference and for the generated schema to appear in the schema references at the end of the swagger.

Teachability, documentation, adoption, migration strategy

Users will be able to not only declare types in swagger's component.schemas via classes but also generate them dynamically as I am doing.

What is the motivation / use case for changing the behavior?

To avoid having to manually write countless DTOs for methods that are standardized but only change the properties of the reference entity, this type of approach would help a lot in the development process.

@leandroluk leandroluk added needs triage This issue has not been looked into type: enhancement 🐺 labels Feb 26, 2025
@kamilmysliwiec
Copy link
Member

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.

@nestjs nestjs locked and limited conversation to collaborators Feb 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into type: enhancement 🐺
Projects
None yet
Development

No branches or pull requests

2 participants