14

I have written an API definition in the OpenAPI 3.0 format (https://swagger.io/docs/specification/basic-structure/). Now I'm trying to generate Java Spring objects as I was previously doing with a Swagger 2.0 definition and its associated Maven plugin.

So far, I have a basic API definition that begins with:

openapi: 3.0.0 info: title: Demo API description: This is a basic REST API implementing the [Open API Specification](https://en.wikipedia.org/wiki/OpenAPI_Specification). version: 0.0.1

In my pom.xml file I have added:

<dependency>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-cli</artifactId>
  <version>3.3.3</version>
</dependency>

But when executing mvn install, I get this error:

com.fasterxml.jackson.core.JsonParseException: Unrecognized
     token 'openapi': was expecting ('true', 'false' or 'null')
     at [Source: definition\DEFINITION.yml; line: 1, column: 9]

Does anyone know where the problem is?

Helen
  • 87,344
  • 17
  • 243
  • 314
vpa
  • 341
  • 1
  • 2
  • 8
  • Is this the actual indentation of your definition.yml or just copy-paste formatting issues? The indentation is wrong - `info` must be on the same level as `openapi`. Paste your YAML into http://editor.swagger.io to make sure the syntax is correct. – Helen Nov 19 '18 at 08:14
  • this is just a copy-paste. My yaml file is correct and no error is displayed when edited in Swagger editor. But it looks like "openapi" tag is not recognise. – vpa Nov 20 '18 at 14:29
  • 1
    This is late and probably wrong, but maybe you should be using openapi-generator-maven-plugin instead of the CLI jar? – chrisinmtown May 01 '19 at 18:47
  • I was getting same exception with openapi spec 3.0.0 and openapi-generator-maven-plugin version 5.10. Upgrading openapi-generator-maven-plugin to version 5.2.1 resolved the issue. – Akshay Joshi Sep 29 '21 at 12:05

7 Answers7

5

After a lot for hit and try I found that issue was with my Swagger file. In that url was not a correct URL. It was something like /api (url without host domain).

OpenAPI generator could have given a better error message. Anyways placing some valid url, allowed me to generate the code.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
1

I had the same problem using io.swagger.codegen.v3.swagger-codegen-maven-plugin and windows (works fine on MacOS - so it couldn't have been a syntax issue).

Upgrading to most recent plugin version did fix the error (3.0.27 at the time of writing).

Jonas
  • 494
  • 4
  • 7
1

For me, it's the folder path that has space in it causing this error.

blackr1234
  • 1,420
  • 12
  • 23
0

I'm currently working on openapi-generator-maven-plugin to generate Java classes from an OpenAPI JSON schema.

The errors looks like a syntax problem. So first make sure your schema is syntactically correct and looks like this:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Demo API",
    "description": "This is a basic REST API implementing the [Open API Specification](https://en.wikipedia.org/wiki/OpenAPI_Specification).",
    "version": "0.0.1"
  },
  # Schema definition goes here
}
julemand101
  • 28,470
  • 5
  • 52
  • 48
R4FT3R
  • 69
  • 1
  • 8
  • 11
    Why should @vpa dumb down his perfectly valid YAML into JSON? – chrisinmtown May 01 '19 at 18:46
  • @chrisinmtown, It makes no logical sense for the user to do that. But since the exception indicates it is trying to use JSON parser to consume YAML input, I can see how this might workaround the issue. – Oliver Gondža Mar 01 '22 at 11:11
0

I ran into the same issue (i need to mention that this occurs only on windows. The same code works fine under linux). While i do not have a "solution"n the workaround which works for me is to set:

validateSpec = false

So something like

generatorName = "typescript-angular"
validateSpec = false
inputSpec = "${myInputSpec}".toString()
outputDir = "${generatedCodeDir}".toString()
matze999
  • 431
  • 1
  • 5
  • 18
0

I received a similar error using the CLI version of OpenAPI Generator. Based on this issue/comment, I believe any validation issue in a YAML spec will trigger the generator to assume the file is JSON.

At least that is what was happening with my file... sadly, I had to use a different tool to validate the YAML, and then OpenAPI Generator worked.

Jason Capriotti
  • 1,836
  • 2
  • 17
  • 33
0

Please change generator plugin and use https://openapi-generator.tech/docs/plugins, i.e.

<plugin>
   <groupId>org.openapitools</groupId>
   <artifactId>openapi-generator-maven-plugin</artifactId>
   <version>6.0.0</version>
   <executions>...</executions>
</plugin>

and my best bet is that it works flawless.

wh81752
  • 869
  • 9
  • 16