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

2

10

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?

Bronez answered 13/5, 2019 at 21:53 Comment(4)
What kind of TypeScript client are you generating?Electrophorus
I thought I had an answer for you but I misunderstood your question. You're trying to avoid the wrapping entirely whereas I though you were only trying to replace inlineObject1 with User.Electrophorus
@ShaunLuttin exactly. if I create a schema explicitly with a name and reference it using $ref, InlineObject1 is replaced by MyNameThatIChoose, but I want to remove wrapping altogether.Tamaru
As far as I can tell, what you're wanting to do is not built-in to any of the existing generators. One approach would be to create your own generator that forks an existing one.Electrophorus
P
12

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 :).

Pollster answered 17/2, 2021 at 14:32 Comment(1)
Can you share example of this behaviour? Thanks ❤️Sooty
W
5

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",
              }
            }
          }
        }
      }
    }
  }
}
Womenfolk answered 12/1, 2022 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.