ad-on-is / adonis-autoswagger

Auto-Generate swagger docs for AdonisJS
MIT License
97 stars 32 forks source link

Wrong @responseBody object scheme generation #101

Closed GilTRipper closed 1 week ago

GilTRipper commented 2 weeks ago
  /*
   * @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}

ad-on-is commented 2 weeks ago

you need to wrap < Post > with quotes "< Post >"

{"post":"< Post>"}

GilTRipper commented 1 week ago
 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

GilTRipper commented 1 week ago

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

ad-on-is commented 1 week ago
 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

GilTRipper commented 1 week ago

It's not normal. Because we generate client api based on this docs. And as a result we have no response type.

ad-on-is commented 1 week ago

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.

GilTRipper commented 1 week ago

Got it What about second question? or better to create new issue?

ad-on-is commented 1 week ago

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.

ad-on-is commented 3 days ago

This is now fixed in the latest version... No need for custom interfaces anymore