19

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?

4
  • 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 editor.swagger.io to make sure the syntax is correct. Commented Nov 19, 2018 at 8: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. Commented Nov 20, 2018 at 14:29
  • 1
    This is late and probably wrong, but maybe you should be using <artifactId>openapi-generator-maven-plugin</artifactId> instead of the CLI jar? Commented May 1, 2019 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. Commented Sep 29, 2021 at 12:05

8 Answers 8

8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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

Comments

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

Comments

1

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.

Comments

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()

Comments

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
}

2 Comments

Why should @vpa dumb down his perfectly valid YAML into JSON?
@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.
0

Faced similar issue with io.swagger.codegen.v3.swagger-codegen-maven-plugin v3.0.35 on windows. It got resolved on updating the dependency to v3.0.55 (latest at time of writing)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.