Openapi-generator: [BUG][tyescript-axios] withSeparateModelsAndApi option throws exception

Created on 15 Jan 2020  路  3Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [x] What's the actual output vs expected output?
Description

I would like to generate a typescript-axios client with separate models. I can successfully generate one without separate ones, but with separate ones, an exception is thrown.

openapi-generator version

4.2.2

OpenAPI declaration file content or url

The spec is dynamically generated by a Nest application (the very reason I want to use a generator to begin with is to keep it in sync). Here's the one that produces this error:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Frontoffice",
    "description": "API docs",
    "version": "0.1",
    "contact": {}
  },
  "tags": [],
  "servers": [],
  "components": {
    "schemas": {
      "RegisterRequestDto": {
        "type": "object",
        "properties": {
          "username": {
            "type": "string",
            "pattern": "^[a-zA-Z0-9]+([a-zA-Z0-9][_\\-][a-zA-Z0-9])*[a-zA-Z0-9]+$"
          },
          "password": {
            "type": "string",
            "minLength": 8
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "phone": {
            "type": "string",
            "description": "Phone number. Can contain arbitrary symbols for readability, but only the digits, and an optional leading plus are accepted.",
            "pattern": "[0-9]+"
          }
        },
        "required": [
          "username",
          "password",
          "email",
          "phone"
        ]
      },
      "PlayerInsertResultDto": {
        "type": "object",
        "properties": {
          "playerId": {
            "type": "number"
          }
        },
        "required": [
          "playerId"
        ]
      },
      "ConflictException": {
        "type": "object",
        "properties": {}
      },
      "UnauthorizedException": {
        "type": "object",
        "properties": {}
      }
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "ApiController-getIndex",
        "responses": {
          "200": {
            "description": ""
          }
        }
      }
    },
    "/register": {
      "get": {
        "operationId": "RegisterApiController-getIndex",
        "responses": {
          "200": {
            "description": ""
          }
        }
      },
      "post": {
        "operationId": "RegisterApiController-postIndex",
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RegisterRequestDto"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PlayerInsertResultDto"
                }
              }
            }
          },
          "409": {
            "description": "If the player already exists",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConflictException"
                }
              }
            }
          }
        }
      }
    },
    "/login": {
      "get": {
        "operationId": "LoginApiController-getIndex",
        "responses": {
          "200": {
            "description": ""
          }
        }
      },
      "post": {
        "operationId": "LoginApiController-postIndex",
        "responses": {
          "201": {
            "description": "A sanitized player object"
          },
          "401": {
            "description": "On invalid credentials",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnauthorizedException"
                }
              }
            }
          }
        }
      }
    },
    "/_schema": {
      "get": {
        "operationId": "SwaggerApiController-getSchema",
        "responses": {
          "200": {
            "description": ""
          }
        }
      }
    },
    "/openapi.json": {
      "get": {
        "operationId": "SwaggerApiController-getDocument",
        "responses": {
          "200": {
            "description": ""
          }
        }
      }
    }
  }
}
Command line used for generation

npx openapi-generator generate -i openapi.json --config .\openapi-generate.json -g typescript-axios -o openapi

Steps to reproduce

  • Set withSeparateModelsAndApi to true in the --config file.
  • Run the command above command.

Output in console:

Exception in thread "main" java.lang.RuntimeException: apiPackage and modelPackage must be defined
        at org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen.processOpts(TypeScriptAxiosClientCodegen.java:117)
        at org.openapitools.codegen.DefaultGenerator.configureGeneratorProperties(DefaultGenerator.java:196)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:913)
        at org.openapitools.codegen.cmd.Generate.run(Generate.java:416)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:61)

as opposed to withSeparateModelsAndApi set to false, which finishes with no errors.

Related issues/PRs
Suggest a fix
TypeScript Bug

Most helpful comment

@boenrobot You need to specify the location the api and models will be created at. If you make two more object keys like below you should be able to make it work.I do think it should have defaults names if you don't provide it, but that is another issue to start.
{ "withSeparateModelsAndApi": true, "apiPackage": "api", "modelPackage": "models" }

If you have no other config options besides that one, you can do it as part of the CLI like
npx openapi-generator generate -i openapi.json -g typescript-axios --additional-properties=withSeparateModelsAndApi=true,modelPackage=models,apiPackage=api -o openapi

All 3 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Bump.

@boenrobot You need to specify the location the api and models will be created at. If you make two more object keys like below you should be able to make it work.I do think it should have defaults names if you don't provide it, but that is another issue to start.
{ "withSeparateModelsAndApi": true, "apiPackage": "api", "modelPackage": "models" }

If you have no other config options besides that one, you can do it as part of the CLI like
npx openapi-generator generate -i openapi.json -g typescript-axios --additional-properties=withSeparateModelsAndApi=true,modelPackage=models,apiPackage=api -o openapi

Was this page helpful?
0 / 5 - 0 ratings