express-typescript-boilerplate icon indicating copy to clipboard operation
express-typescript-boilerplate copied to clipboard

Add openApiGenerator and class-schema-validator

Open gualtierim opened this issue 6 years ago • 14 comments

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!

gualtierim avatar Jan 03 '19 14:01 gualtierim

Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @Type(() => User)

They dont seem to work. image

somehowchris avatar Jan 08 '19 13:01 somehowchris

Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @type(() => User)

They dont seem to work. image

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.

gualtierim avatar Jan 08 '19 17:01 gualtierim

Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @type(() => User) They dont seem to work. image

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

somehowchris avatar Jan 08 '19 17:01 somehowchris

Thnx. Tested some stuff with it. Added those to my relations @ValidateNested({ each: true }) @type(() => User) They dont seem to work. image

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.

gualtierim avatar Jan 08 '19 17:01 gualtierim

@somehowchris Have you tried the last changes?

gualtierim avatar Jan 22 '19 21:01 gualtierim

Yes and it works. But im struggling with adding token auth to the endpoints.

somehowchris avatar Jan 22 '19 21:01 somehowchris

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

epiphone avatar Jan 23 '19 07:01 epiphone

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: [],
    },
];

somehowchris avatar Jan 25 '19 11:01 somehowchris

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.

epiphone avatar Feb 14 '19 06:02 epiphone

Nice 👌

somehowchris avatar Feb 14 '19 06:02 somehowchris

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

somehowchris avatar May 10 '19 10:05 somehowchris

@gualtierim @epiphone Its happens to me in 8 of 8 projects

somehowchris avatar May 10 '19 14:05 somehowchris

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.

somehowchris avatar May 10 '19 15:05 somehowchris

@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
        ],

somehowchris avatar May 10 '19 15:05 somehowchris