Search code examples
swaggeropenapiopenapi-generatoropenapi-generator-cli

openapi-generator generates empty interface


I am trying to generate TS stubs for the API of the backend services. My problem is that it generates an empty interface which I cannot use in my TypeScript code...

Here is the spec:

{
  "components": {
    "schemas": {
      "Schema1": {
        "properties": {
          "metric": {
            "type": "array"
          }
        }
      },
      "Schema2": {
        "properties": {
          "address": {
            "anyOf": [
              {
                "type": "string"
              }
            ],
            "title": "Address"
          }
        }
      }
    }
  },
  "info": {
    "title": "Test API",
    "version": "v1.11.1"
  },
  "openapi": "3.1.0"
}

Here is the command-line:

java -jar 7.1.0.jar generate --input-spec=api.json --generator-name="typescript-fetch" --output="tmp/"

And the interfaces produced (combining two files here and extracting only the relevant stuff):

export interface Address {
}

export interface Schema2 {
    address?: Address;
}

Address is supposed to be a string, here it is mapped to an empty interface. Oddly enough, if I remove Schema1 from the spec, the code generated is correct:

export interface Schema2 {
    address?: string;
}

Any idea what I am doing wrong ?


Solution

  • As specified in the swagger spec, the items keyword is required in arrays, and my spec didn't have any. As a matter of fact, the generation always displayed an exception, and I didn't think it was related:

    java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because the return value of "io.swagger.v3.oas.models.media.Schema.getItems()" is null
        at org.openapitools.codegen.OpenAPINormalizer.processNormalize31Spec(OpenAPINormalizer.java:902)
        at org.openapitools.codegen.OpenAPINormalizer.normalizeSimpleSchema(OpenAPINormalizer.java:446)
        at org.openapitools.codegen.OpenAPINormalizer.normalizeSchema(OpenAPINormalizer.java:437)
    ...
    

    Typing the array in Schema1 fixes the issue, and the exception is not thrown anymore when generating the stubs:

    {
      "components": {
        "schemas": {
          "Schema1": {
            "properties": {
              "metric": {
                "type": "array",
                "items": {
                  "type": "string"  
                }
              }
            }
          },
          "Schema2": {
            "properties": {
              "address": {
                "type": "string"
              }
            }
          }
        }
      },
      "info": {
        "title": "Test API",
        "version": "v1.11.1"
      },
      "openapi": "3.1.0"
    }