From 910d86e983a1cb46e92c6e3690c18d874be1f79e Mon Sep 17 00:00:00 2001
From: Jason henriksen <jason.henriksen@gmail.com>
Date: Fri, 22 Feb 2019 18:59:27 -0800
Subject: [PATCH] modified the meta command to generate an easy to use method
 for debugging new libraries

---
 .../org/openapitools/codegen/cmd/Meta.java    |  1 +
 .../main/resources/codegen/README.mustache    |  3 ++
 .../codegen/debugGeneratorTest.mustache       | 32 +++++++++++++++++++
 .../resources/codegen/generatorClass.mustache | 26 +++++++++++++++
 .../src/main/resources/codegen/pom.mustache   | 10 ++++++
 5 files changed, 72 insertions(+)
 create mode 100644 modules/openapi-generator/src/main/resources/codegen/debugGeneratorTest.mustache

diff --git a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java
index c373aaa8c70..8cee440fc3b 100644
--- a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java
+++ b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java
@@ -83,6 +83,7 @@ public class Meta implements Runnable {
                 ImmutableList.of(
                         new SupportingFile("pom.mustache", "", "pom.xml"),
                         new SupportingFile("generatorClass.mustache", on(File.separator).join("src/main/java", asPath(targetPackage)), mainClass.concat(".java")),
+                        new SupportingFile("debugGeneratorTest.mustache", on(File.separator).join("src/test/java", asPath("org.openapitools.codegen.debug")), "DebugCodegenLauncher.java"),
                         new SupportingFile("README.mustache", "", "README.md"),
                         new SupportingFile("api.template", "src/main/resources" + File.separator + name,"api.mustache"),
                         new SupportingFile("model.template", "src/main/resources" + File.separator + name,"model.mustache"),
diff --git a/modules/openapi-generator/src/main/resources/codegen/README.mustache b/modules/openapi-generator/src/main/resources/codegen/README.mustache
index 7b274871839..c55f0b6ce13 100644
--- a/modules/openapi-generator/src/main/resources/codegen/README.mustache
+++ b/modules/openapi-generator/src/main/resources/codegen/README.mustache
@@ -64,6 +64,9 @@ The `{{generatorClass}}.java` has comments in it--lots of comments.  There is no
 for reading the code more, though.  See how the `{{generatorClass}}` implements `CodegenConfig`.
 That class has the signature of all values that can be overridden.
 
+You can also step through {{generatorClass}}.java in a debugger.  Just debug the JUnit
+test in DebugCodegenLauncher.  That runs the command line tool and lets you inspect what the code is doing.  
+
 For the templates themselves, you have a number of values available to you for generation.
 You can execute the `java` command from above while passing different debug flags to show
 the object you have available during client generation:
diff --git a/modules/openapi-generator/src/main/resources/codegen/debugGeneratorTest.mustache b/modules/openapi-generator/src/main/resources/codegen/debugGeneratorTest.mustache
new file mode 100644
index 00000000000..2374401a623
--- /dev/null
+++ b/modules/openapi-generator/src/main/resources/codegen/debugGeneratorTest.mustache
@@ -0,0 +1,32 @@
+package org.openapitools.codegen.debug;
+
+import org.junit.Test;
+import org.openapitools.codegen.OpenAPIGenerator;
+
+/***
+ * This test allows you to easily launch your code generation software under a debugger.
+ * Then run this test under debug mode.  You will be able to step through your java code 
+ * and then see the results in the out directory. 
+ *
+ * To experiment with debugging your code generator:
+ * 1) Set a break point in {{generatorClass}}.java in the postProcessOperationsWithModels() method.
+ * 2) To launch this test in Eclipse: right-click | Debug As | JUnit Test
+ *
+ */
+public class DebugCodegenLauncher 
+{
+  @Test
+  public void launchCodeGeneratorInDebugMode()
+  {
+    // use this test to launch you code generator in the debugger.
+    // this allows you to easily set break points in {{generatorClass}}.
+    String commandLineParams =
+        "generate "+              
+        "-i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml "+ // sample swagger 
+        "-t ./src/main/resources/{{name}} "+          // template directory
+        "-o out/{{name}} "+                           // output directory
+        "-g {{name}} ";                               // use this codegen library
+    
+    OpenAPIGenerator.main( commandLineParams.split(" ") );
+  }
+}
diff --git a/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache b/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache
index cefd218cb50..ef2c2b0778b 100644
--- a/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache
+++ b/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache
@@ -31,6 +31,32 @@ public class {{generatorClass}} extends DefaultCodegen implements CodegenConfig
   public String getName() {
     return "{{name}}";
   }
+  
+  /**
+   * Provides an opportunity to inspect and modify operation data before the code is generated.
+   */
+  @SuppressWarnings("unchecked")
+  @Override
+  public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
+	
+    // to try debugging your code generator:
+    // set a break point on the next line.
+    // then debug the JUnit test called LaunchGeneratorInDebugger
+    
+    Map<String, Object> results = super.postProcessOperationsWithModels(objs, allModels);
+    
+    Map<String, Object> ops = (Map<String, Object>)results.get("operations");
+    ArrayList<CodegenOperation> opList = (ArrayList<CodegenOperation>)ops.get("operation");
+    
+    // iterate over the operation and perhaps modify something
+    for(CodegenOperation co : opList){
+      // example:
+      // co.httpMethod = co.httpMethod.toLowerCase();
+    }
+		
+    return results;
+  }
+  
 
   /**
    * Returns human-friendly help for the generator.  Provide the consumer with help
diff --git a/modules/openapi-generator/src/main/resources/codegen/pom.mustache b/modules/openapi-generator/src/main/resources/codegen/pom.mustache
index cb3712e87ad..672f3813c42 100644
--- a/modules/openapi-generator/src/main/resources/codegen/pom.mustache
+++ b/modules/openapi-generator/src/main/resources/codegen/pom.mustache
@@ -113,6 +113,16 @@
             <version>${openapi-generator-version}</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.openapitools</groupId>
+            <artifactId>openapi-generator-cli</artifactId>
+            <version>${openapi-generator-version}</version>
+        </dependency>        
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>${junit-version}</version>
+        </dependency>         
     </dependencies>
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-- 
GitLab