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

Add openApiGenerator and class-schema-validator #141

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

gualtierim
Copy link
Contributor

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:

express-typescript-boilerplate@3.2.0 start /Library/WebServer/Documents/express-typescript-boilerplate
nps

/Library/WebServer/Documents/express-typescript-boilerplate/package-scripts.js:41
),

Thanks!

@somehowchris
Copy link

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

They dont seem to work.
image

@gualtierim
Copy link
Contributor Author

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.

@somehowchris
Copy link

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

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@gualtierim
Copy link
Contributor Author

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
Copy link
Contributor Author

@somehowchris Have you tried the last changes?

@somehowchris
Copy link

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

@epiphone
Copy link
Contributor

epiphone commented Jan 23, 2019

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

@somehowchris
Copy link

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

@epiphone
Copy link
Contributor

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.

@somehowchris
Copy link

Nice 👌

@somehowchris
Copy link

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
Copy link

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

@somehowchris
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants