Wrong @responseBody object scheme generation
Closed this issue · 9 comments
/*
* @store
* @requestBody - <createPostValidator>
* @responseBody 200 - {"post": <Post>}
*/
async store({ auth, request }: HttpContext) {
const user = auth.getUserOrFail()
const data = await request.validateUsing(createPostValidator)
const post = await this.postService.create(data, user)
return { post }
}
in this example we want response to be JSON object with post field that includes PostScheme
but in the end we have this scheme
post:
summary: " (store)"
description: "\n\n _app/controllers/api/posts_controller.ts_ - **store**"
parameters: []
tags:
- "POSTS"
responses:
200:
content:
application/json:
schema:
$ref: "#/components/schemas/Post"
example:
id: 742
title: "Lorem Ipsum"
description: "Lorem ipsum dolor sit amet"
user_id: 978
created_at: "2021-03-23T16:13:08.489+01:00"
updated_at: "2021-03-23T16:13:08.489+01:00"
so it must be not just Post model response but JSON: {post: Post}
you need to wrap < Post > with quotes "< Post >"
{"post":"< Post>"}
post:
summary: " (store)"
description: "\n\n _app/controllers/api/posts_controller.ts_ - **store**"
parameters: []
tags:
- "POSTS"
responses:
200:
content:
application/json:
schema:
type: "object"
example:
post:
id: 669
title: "Lorem Ipsum"
description: "Lorem ipsum dolor sit amet"
user_id: 962
created_at: "2021-03-23T16:13:08.489+01:00"
updated_at: "2021-03-23T16:13:08.489+01:00"
when i wrap < Post > in double quotes its okay with example. But schema becomes any object
Btw have one more issue with rendering docs.
return AutoSwagger.default.docs(router.toJSON(), swagger)
the code above throws an error that validator/post.ts doesn't exist
but when running command
node ace docs:generate
everything is fine and all validators are passed to schema
post: summary: " (store)" description: "\n\n _app/controllers/api/posts_controller.ts_ - **store**" parameters: [] tags: - "POSTS" responses: 200: content: application/json: schema: type: "object" example: post: id: 669 title: "Lorem Ipsum" description: "Lorem ipsum dolor sit amet" user_id: 962 created_at: "2021-03-23T16:13:08.489+01:00" updated_at: "2021-03-23T16:13:08.489+01:00"
when i wrap < Post > in double quotes its okay with example. But schema becomes any object
That's normal, because I'd have to generate a new schema for each and every custom json that is provided. This is out of scope
It's not normal. Because we generate client api based on this docs. And as a result we have no response type.
It's not normal. Because we generate client api based on this docs. And as a result we have no response type.
What I meant by normal is , you did not do anything wrong here. 😅
I didn't know this would blow up so much that people would use it to autogenerate other stuff based on this one.
If you need it, fell free to submit a PR.
Got it
What about second question?
or better to create new issue?
Alternatively, you can create an Interface and use that instead of {"post": "< Post>"}
export interface PostInterface {
post: Post
}
and then
@responseBody 200 - <PostInterface>
Yeah, please create a separate issue for the other one. with the error that it produces, and the validator you use.
This is now fixed in the latest version... No need for custom interfaces anymore