ferdikoomen / openapi-typescript-codegen

NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification
MIT License
2.88k stars 520 forks source link

Models are only generated for schemas within the components object #1475

Open creativecreature opened 1 year ago

creativecreature commented 1 year ago

Describe the bug It does not generate models for schemas that are defined within the paths object:

  "openapi": "3.0.3",
  "info": {
    "title": "Example API",
    "version": "1.0.0"
  },
  "paths": {
    "/v1/example": {
      "get": {
        "responses": {
          "200": {
            "description": "GET /v1/examples Returns all examples",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string"
                          }
                        },
                        "required": ["id"]
                      }
                    }
                  },
                  "required": ["data"]
                }
              }
            }
          }
        },
        "parameters": []
      }
    },

...

  "components": {
    "schemas": {},
    "responses": {},
    "parameters": {},
    "examples": {},
    "requestBodies": {},
    "headers": {},
    "securitySchemes": {},
    "links": {},
    "callbacks": {}
  }

The models are only generated if the schemas are moved into components/schemas at the bottom of the file.

Would it be possible to modify the getModels function so that it traverses the paths and looks for schemas there too? Right now it only looks for schemas within components/schemas and components/parameters: https://github.com/ferdikoomen/openapi-typescript-codegen/blob/master/src/openApi/v3/parser/getModels.ts#L9

The services that we are able to generate with this library can extract types from the schemas that are defined within the paths. This makes me hopeful that most of the code to achieve this could exist within this project already.

I guess the tricky part would be to figure out what these models should be named.

jvh-iodigital commented 1 year ago

This would be useful for our use-case as well. The title property of inlined schemas can be used to determine the model name.

eg:

 "responses": {
   "200": {
     "description": "GET /v1/examples Returns all examples",
     "content": {
       "application/json": {
         "schema": {
           "title": "InlineModel"
           "type": "object",
           "properties": {
             "data": {
               "type": "array",
               "items": {
                 "type": "object",
                 "properties": {
                   "id": {
                  ...