Swagger-codegen: swagger codegen cli for openspec 3 - java language generation fails

Created on 22 May 2018  路  6Comments  路  Source: swagger-api/swagger-codegen

Description

When I try to generate java stubs from a simple yml api definition I am getting the following error.

I am able to generate using other language types - but were hoping that the "java" language option will yield a set of stubs that are framework agnostic. I would like the generated stubs to be simply the model and an interface for the service.

Exception in thread "Thread-1" java.lang.RuntimeException: Could not generate model 'AppConfig'
at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:409)
at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:728)
at io.swagger.codegen.cmd.Generate.run(Generate.java:331)
at java.lang.Thread.run(Unknown Source)
Caused by: java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 3
v2\Java
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.escape(Unknown Source)
at java.util.regex.Pattern.atom(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceFirst(Unknown Source)
at io.swagger.codegen.DefaultGenerator.getHandlebars(DefaultGenerator.java:1014)
at io.swagger.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:744)
at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:396)
... 3 more

Swagger-codegen version

swagger-codegen-cli-3.0.0-20180515.103456-67.jar

Swagger declaration file content or url
openapi: 3.0.0
servers:
# Added by API Auto Mocking Plugin
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/test/1.0.0  
info:
  description: Sample API
  version: "1.0.0"
  title: Sample API
  contact:
    email: [email protected]
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
  - name: admins
    description: Secured Admin-only calls
  - name: developers
    description: Operations available to regular developers
paths:
  /health:
    get:
      tags:
        - developers
      summary: Health endpoint
      operationId: health
      responses:
        '200':
          description: Health metrics for this endpoint
          content:
            application/json:
              schema:
                type: object                
                $ref: '#/components/schemas/HealthMetric'   
components:
  schemas:
    HealthMetric:
      type: object
      required:
        - reportGenerationEnabled
        - applicationVersion
        - configuration
        - hostname
      properties:
        reportGenerationEnabled:
          type: boolean
        applicationVersion:
          type: string
          example: "<artifact group>:<artifact name>:<artifact version>"
        hostname:
          type: string
        appConfig:
          $ref: '#/components/schemas/AppConfig'
    AppConfig:
      type: object
      required:
         - name
      properties:
        name:
          type: string
          description: A string       
Command line used for generation

java -jar swagger-codegen-cli-3.0.0-20180515.103456-67.jar generate -l java -i sample.yml -o ./dest

Steps to reproduce

Just run the command above.

Related issues/PRs
Suggest a fix/enhancement

Most helpful comment

Replacing the replaceFirst() with StringUtils.replaceOnce() in DefaultGenerator:1014 fixes the problem for Windows...

    private com.github.jknack.handlebars.Template getHandlebars(String templateFile) throws IOException {
        if (templateFile.startsWith(config.templateDir())) {
            // templateFile = templateFile.replaceFirst(config.templateDir(), StringUtils.EMPTY);
            templateFile = StringUtils.replaceOnce(templateFile, config.templateDir(), StringUtils.EMPTY);
        }

All 6 comments

The issues seems to be in DefaultGenerator.java line 1013 method getHandlebars.

The first part of this method has teh logic:

if (tempalteFile.startsWith(config.templateDir())) {
templateFile = tempalteFile.replaceFirst(config.templateDir(0, StringUtils.Empty);
}

template file is: v2\Java\model.mustache
template dir is: v2\Java

This only happens in Windows for me, and I think has to do with the backslashes being misinterpreted in the replaceFirst() first parameter which is supposed to be a regex string...

I have received the same error reliably in Windows, although the same commands work fine in macOs.

swagger-codegen-cli-3.0.0-20180523.181249-69.jar

My command line is

java -jar swagger-codegen-cli-3.0.0-SNAPSHOT.jar generate -i ./spec/openapi.yaml -l jaxrs-cxf -Dmodels --model-package com.testing.models -t C:\TestCodeGen\codegen\template

And my error message is:

Thread-1] INFO io.swagger.codegen.ignore.CodegenIgnoreProcessor - No .swagger-codegen-ignore file found.
[Thread-1] INFO io.swagger.codegen.languages.java.AbstractJavaCodegen - Invoker Package Name, originally not set, is now dervied from model package name: com.grainger.keepstock
Exception in thread "Thread-1" java.lang.RuntimeException: Could not generate model 'Audit'
        at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:409)
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:728)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:331)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 3
C:\TestCodeGen\codegen\template
   ^
        at java.util.regex.Pattern.error(Unknown Source)
        at java.util.regex.Pattern.escape(Unknown Source)
        at java.util.regex.Pattern.atom(Unknown Source)
        at java.util.regex.Pattern.sequence(Unknown Source)
        at java.util.regex.Pattern.expr(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
        at java.util.regex.Pattern.<init>(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
        at java.lang.String.replaceFirst(Unknown Source)
        at io.swagger.codegen.DefaultGenerator.getHandlebars(DefaultGenerator.java:1014)
        at io.swagger.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:744)
        at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:396)
        ... 3 more

This happens in Windows 7 and Windows 10, JRE 8 or 9.

I will try to make a local build and fix the line.

Replacing the replaceFirst() with StringUtils.replaceOnce() in DefaultGenerator:1014 fixes the problem for Windows...

    private com.github.jknack.handlebars.Template getHandlebars(String templateFile) throws IOException {
        if (templateFile.startsWith(config.templateDir())) {
            // templateFile = templateFile.replaceFirst(config.templateDir(), StringUtils.EMPTY);
            templateFile = StringUtils.replaceOnce(templateFile, config.templateDir(), StringUtils.EMPTY);
        }

@bigtlb - swagger-codegen 3.0.0 rc1 does not seem to have this fix. Do you know if it made to that release candidate?
Running into this same exact issue and cant seem to download latest source with that line.

hey @bigtlb @murthy5 can you please try with https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.0 and let me know if there is something wrong?

I have added a fix for this issue in another GitHub issue which is about the same topic.
#8273

Was this page helpful?
0 / 5 - 0 ratings