Search code examples
swaggeropenapiswagger-codegenopenapi-generatorswagger-codegen-maven-plugin

Swagger Codegen for OAS3


I'm trying to use Swagger Codegen for my OAS3 project. I have cloned swagger-codegen branch 3.0.0. and when I run the generate command I get the following error:

[main] ERROR io.swagger.parser.SwaggerCompatConverter - failed to read resource listing
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'openapi': was expecting ('true', 'false' or 'null')

 at [Source: (String)"openapi: 3.0.0

Does Codegen supports openapi 3.0.1 yet? If yes what am I missing ?

UPDATE - Yaml file and codegen command

Petstore.yaml file :

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

Codegen command:

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

java -jar ./swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i "./definitions/petstoreV3.yaml" -l javascript -o "$SCRIPT_DIR/.swagger_gen_temp"

Solution

  • The error occurs because you are running V2 codegen (from the master branch) instead of V3 codegen (from the 3.0.0 branch). V2 codegen does not support OpenAPI 3.0.

    You can download the JAR of V3 codegen from:
    http://central.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.11/swagger-codegen-cli-3.0.11.jar

    V3 codegen, however, does not currently have a JavaScript client generator.