express-typescript-boilerplate
express-typescript-boilerplate copied to clipboard
Add openApiGenerator and class-schema-validator
Hi,
I added routing-controllers-openapi
in order to create dinamically the swaggerFile. The static swagger.json
now is no more necessary.
When running the command npm run start
nps throught the following error, but this error is not due to changes made:
[email protected] start /Library/WebServer/Documents/express-typescript-boilerplate nps
/Library/WebServer/Documents/express-typescript-boilerplate/package-scripts.js:41 ),
Thanks!
Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @Type(() => User)
They dont seem to work.
Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @type(() => User)
They dont seem to work.
It seems to be a error of the library class-validator-jsonschema
. In my opinion, the generation of the jsonschema is not recursive. I tried the decorator @isIstance
, but it doesn't work too.
Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @type(() => User) They dont seem to work.
It seems to be a error of the library
class-validator-jsonschema
. In my opinion, the generation of the jsonschema is not recursive. I tried the decorator@isIstance
, but it doesn't work too.
Well I just found out the it is caused by the configuration in the meta data of the schema references. Its missing a slash (/). Seems to work. Still testing some things though
Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @type(() => User) They dont seem to work.
It seems to be a error of the library
class-validator-jsonschema
. In my opinion, the generation of the jsonschema is not recursive. I tried the decorator@isIstance
, but it doesn't work too.Well I just found out the it is caused by the configuration in the meta data of the schema references. Its missing a slash (/). Seems to work. Still testing some things though
I found the error. Now should work fine.
@somehowchris Have you tried the last changes?
Yes and it works. But im struggling with adding token auth to the endpoints.
struggling with adding token auth to the endpoints.
Hi, was the problem including the security
definition on operation objects?
"security": [
{
"basicAuth": []
}
]
We can handle that with the OpenAPI
decorator:
import { OpenAPI } from 'routing-controllers-openapi'
@Post()
@OpenAPI({ security: [{ basicAuth: [] }] })
public create(@Body() pet: Pet): Promise<Pet> {
// ...
}
If adding the same OpenAPI
to each endpoint is tedious, we could wrap it in a new decorator:
const OpenAPIBasicAuth = OpenAPI({ security: [{ basicAuth: [] }] })
@Post()
@OpenAPIBasicAuth
public create(@Body() pet: Pet): Promise<Pet> {
// ...
}
Alternatively I was thinking we could extend OpenAPI
to support decorating controller classes, so that definitions flow down to each endpoint function (edit this is now published in 1.6.0
)
@Authorized()
@JsonController('/users')
@OpenAPI({ security: [{ basicAuth: [] }] })
export class UserController {
// ...
What do you think? @somehowchris
Thanks for your help. I made some mistake in the global security definition. No my configuration of my swagger file looks like that
components: {
schemas,
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'bearer',
},
},
},
swaggerFile.security = [
{
bearerAuth: [],
},
];
Just a heads-up, routing-controllers-openapi
now supports class-level decorators which helps to reduce duplication in cases where e.g. each method needs an identical security
definition.
Nice 👌
Im running again into a problem. I have a UserSerivce which has a TokenRepository, a UserRepository and the Logger in its constructor. Im trying to load it in the currentUserChecker function of routing controllers.
const userService = Container.get<UserService>(UserService);
If have it there then the MetaStorage seems to be empty. And therefor no entities are showing in the swagger.
I've nailed it down to this line in the constructor
@InjectRepository(User) private userRepository: Repository<User>
I've imported the things correctly and also tried it with @OrmRepository.
Thanks for your help
@gualtierim @epiphone Its happens to me in 8 of 8 projects
Has todo with the useContainer function in iocLoader.
Small workaround: Get the metadata before and set it into the settings, load it in the swaggerLoader and merge it with the container metadata.
@gualtierim @epiphone Other thing. Sometimes I get those warnings:
Resolver error at paths./mails/templates/{id}.put.requestBody.content.application/json.schema.properties.user.$ref
Could not resolve reference: #/components/schemas/Object
Resolver error at paths./user/{id}.put.responses.200.content.application/json.schema.properties.permittedSurveys.items.properties.questions.items.$ref
Could not resolve reference: #/components/schemas/Array
Any ideas why? Or sometimes example references seem to be null in the json edit view of swagger
"users": [
null
],
"permittedSurveys": [
null
],
"sharedSurveys": [
null
],