Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid wrapping request object into InlineObject1 in OpenAPI Generator from OpenAPI 3.0 spec to Typescript

I am trying to generate Typescript client with OpenAPI Generator 4.0.0-SNAPSHOT (our manager asked us to use that version for some reason) that reads an OpenAPI 3.0 spec. All of our current endpoints either accept data in query string or as form body, and they all work perfectly. I have a new endpoint that will read data as JSON in POST body (other endpoints will eventually be converted too). It will accept an object like the following:

{email: "[email protected]"}

I'm trying to document the endpoint in YAML as following:

 /users/send-password-reminder:
    post:
      [...]
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                email:
                  type: string
      responses:
        [...]

However, when I generate the Typescript client, it generates a SendPasswordReminderRequest object, which wraps an autogenerated InlineObject1 object, which wraps my actual email.

This causes me to use it like:

const req: SendPasswordReminderRequest = {
    inlineObject1:{
      email: "..."
  }
};

APIClient.user.sendPasswordReminder(req, ...)

What I want instead is to get rid of that InlineObject1 completely and make SendPasswordReminderRequest directly wrap email property, and to be able to use it as:

const req: SendPasswordReminderRequest = {
   email: "..."
};

APIClient.user.sendPasswordReminder(req, ...)

I've tried defining the body in components/requestBodies and using $ref, and it still wraps the actual body even though it uses the name of my request body type.

How can I get rid of this wrapping?

like image 415
Can Poyrazoğlu Avatar asked May 13 '19 21:05

Can Poyrazoğlu


2 Answers

Im not sure if this will be still relevant for you but in code I find a way how to resolve not to generate InlineObjects at all

If you define "title" for your schema used in requestBody, it will use this title to name inline object.

So instead of getting InlineObject You can get something similar to: sendPasswordReminderRequestData, based on how you name your schema.

Hope this helps You or anyone else :).

like image 164
Tomáš Zábranský Avatar answered Oct 16 '22 16:10

Tomáš Zábranský


Here is an example of how to add the title to a POST request so it is generated with a friendly name via client generators (other data omitted for brevity):

{
  "openapi": "3.0.1",
  "paths": {
    "/Users/Hello": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "title": "UsersHelloRequest",
              }
            }
          }
        }
      }
    }
  }
}
like image 3
joshcomley Avatar answered Oct 16 '22 18:10

joshcomley