swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.08k stars 6.03k forks source link

swagger codegen cli for openspec 3 - java language generation fails #8218

Open reubentonna opened 6 years ago

reubentonna commented 6 years ago
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: test@email.com
  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
reubentonna commented 6 years ago

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

bigtlb commented 6 years ago

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.

bigtlb commented 6 years ago

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);
        }
murthy5 commented 6 years ago

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

HugoMario commented 6 years ago

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?

PetervLeeuwen commented 5 years ago

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

megretson commented 2 years ago

Hello, I am still experiencing this issue with the following versions on Windows: swagger-codegen: 3.0.8

Frustrating that this is still an open issue 3 years later. Here's my stack trace:

[ERROR] java.lang.RuntimeException: Could not generate model 'CollimatorType' at io.swagger.codegen.DefaultGenerator.generateModels (DefaultGenerator.java:409) at io.swagger.codegen.DefaultGenerator.generate (DefaultGenerator.java:728) at io.swagger.codegen.plugin.CodeGenMojo.execute (CodeGenMojo.java:534) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager .java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModule Builder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModule Builder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build ( SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293) at org.apache.maven.cli.MavenCli.main (MavenCli.java:196) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347) Caused by: java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near ind ex 3 v2\Java ^ at java.util.regex.Pattern.error (Pattern.java:1969) at java.util.regex.Pattern.escape (Pattern.java:2485) at java.util.regex.Pattern.atom (Pattern.java:2212) at java.util.regex.Pattern.sequence (Pattern.java:2144) at java.util.regex.Pattern.expr (Pattern.java:2010) at java.util.regex.Pattern.compile (Pattern.java:1702) at java.util.regex.Pattern.<init> (Pattern.java:1352) at java.util.regex.Pattern.compile (Pattern.java:1028) at java.lang.String.replaceFirst (String.java:2178) 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) at io.swagger.codegen.DefaultGenerator.generate (DefaultGenerator.java:728) at io.swagger.codegen.plugin.CodeGenMojo.execute (CodeGenMojo.java:534) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager .java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModule Builder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModule Builder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build ( SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293) at org.apache.maven.cli.MavenCli.main (MavenCli.java:196) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)