|
| 1 | +### Swagger Codegen 3.0.0 Standalone generator development (separate project/repo) |
| 2 | + |
| 3 | +As described in [Readme](https://github.com/swagger-api/swagger-codegen/tree/3.0.0#making-your-own-codegen-modules), |
| 4 | +a new generator can be implemented by starting with a project stub generated by the `meta` command of `swagger-codegen-cli`. |
| 5 | + |
| 6 | +This can be achieved without needing to clone the `swagger-codegen` repo, by downloading and running the jar, e.g.: |
| 7 | + |
| 8 | +``` |
| 9 | +wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.19/swagger-codegen-cli-3.0.19.jar -O swagger-codegen-cli.jar |
| 10 | +java -jar swagger-codegen-cli.jar meta -o output/myLibrary -n myClientCodegen -p com.my.company.codegen |
| 11 | +``` |
| 12 | + |
| 13 | +Some additional details about codegen APIs of interest to the generator Java class(es) are [available here](https://github.com/swagger-api/swagger-codegen-generators/wiki/Adding-a-new-generator-for-a-language-or-framework. |
| 14 | +). |
| 15 | + |
| 16 | +Such generator can be then made available to the CLI by adding it to the classpath, allowing to run/test it via the command line CLI, |
| 17 | +add it to the build pipeline and so on, as mentioned in [Readme](https://github.com/swagger-api/swagger-codegen/tree/3.0.0#making-your-own-codegen-modules). |
| 18 | + |
| 19 | +#### Adding tests |
| 20 | + |
| 21 | +One easy way to test the generator outcome without going through the CLI is adding a test like: |
| 22 | + |
| 23 | + |
| 24 | +```java |
| 25 | + |
| 26 | +@Test |
| 27 | +public void testGenerator() throws Exception { |
| 28 | + |
| 29 | + String path = getOutFolder(false).getAbsolutePath(); |
| 30 | + GenerationRequest request = new GenerationRequest(); |
| 31 | + request |
| 32 | + .codegenVersion(GenerationRequest.CodegenVersion.V3) // use V2 to target Swagger/OpenAPI 2.x Codegen version |
| 33 | + .type(GenerationRequest.Type.CLIENT) |
| 34 | + .lang("theNameOfMyCodegen") |
| 35 | + .spec(loadSpecAsNode( "theSpecIWantToTest.yaml", |
| 36 | + true, // YAML file, use false for JSON |
| 37 | + false)) // OpenAPI 3.x - use true for Swagger/OpenAPI 2.x definitions |
| 38 | + .options( |
| 39 | + new Options() |
| 40 | + .outputDir(path) |
| 41 | + ); |
| 42 | + |
| 43 | + List<File> files = new GeneratorService().generationRequest(request).generate(); |
| 44 | + Assert.assertFalse(files.isEmpty()); |
| 45 | + for (File f: files) { |
| 46 | + // test stuff |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + protected static File getOutFolder(boolean delete) { |
| 51 | + try { |
| 52 | + File outputFolder = new File("customCodegenOut"); |
| 53 | + System.out.println(outputFolder.getAbsolutePath()); |
| 54 | + if (delete) { |
| 55 | + // delete.. |
| 56 | + } |
| 57 | + return outputFolder; |
| 58 | + } catch (Exception e) { |
| 59 | + e.printStackTrace(); |
| 60 | + return null; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected JsonNode loadSpecAsNode(final String file, boolean yaml, boolean v2) { |
| 65 | + InputStream in = null; |
| 66 | + String s = ""; |
| 67 | + try { |
| 68 | + in = getClass().getClassLoader().getResourceAsStream(file); |
| 69 | + if (yaml) { |
| 70 | + if (v2) { |
| 71 | + return Yaml.mapper().readTree(in); |
| 72 | + } else { |
| 73 | + return io.swagger.v3.core.util.Yaml.mapper().readTree(in); |
| 74 | + } |
| 75 | + } |
| 76 | + if (v2) { |
| 77 | + return Json.mapper().readTree(in); |
| 78 | + } |
| 79 | + return io.swagger.v3.core.util.Json.mapper().readTree(in); |
| 80 | + } catch (Exception e) { |
| 81 | + throw new RuntimeException("could not load file " + file); |
| 82 | + } finally { |
| 83 | + IOUtils.closeQuietly(in); |
| 84 | + } |
| 85 | + } |
| 86 | +``` |
| 87 | + |
| 88 | +#### Development in docker |
| 89 | + |
| 90 | +Similar to what mentioned in Readme [development in docker section](https://github.com/swagger-api/swagger-codegen/tree/3.0.0#development-in-docker), a standalone generator can be built and run in docker, without need of a java/maven environment on the local machine. |
| 91 | + |
| 92 | +Generate the initial project: |
| 93 | + |
| 94 | +```bash |
| 95 | + |
| 96 | +# project dir |
| 97 | +TARGET_DIR=/tmp/codegen/mygenerator |
| 98 | +mkdir -p $TARGET_DIR |
| 99 | +cd $TARGET_DIR |
| 100 | +# generated code location |
| 101 | +GENERATED_CODE_DIR=generated |
| 102 | +mkdir -p $GENERATED_CODE_DIR |
| 103 | +# download desired version |
| 104 | +wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.19/swagger-codegen-cli-3.0.19.jar -O swagger-codegen-cli.jar |
| 105 | +wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/docker-stub.sh -O docker-stub.sh |
| 106 | +wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/generator-stub-docker.sh -O generator-stub-docker.sh |
| 107 | +chmod +x *.sh |
| 108 | +# generated initial stub: -p <root package> -n <generator name> |
| 109 | +./generator-stub-docker.sh -p io.swagger.codegen.custom -n custom |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | +A test definition if we don't have one: |
| 114 | + |
| 115 | +```bash |
| 116 | +wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/modules/swagger-codegen/src/test/resources/3_0_0/petstore-simple.yaml -O petstore-simple.yaml |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +Build the generator and run it against a definition (first time will be slower as it needs to download deps) |
| 121 | + |
| 122 | +```bash |
| 123 | +wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/run-in-docker.sh -O run-in-docker.sh |
| 124 | +wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/docker-entrypoint.sh -O docker-entrypoint.sh |
| 125 | +chmod +x *.sh |
| 126 | +./run-in-docker.sh generate -i petstore-simple.yaml -l custom -o /gen/$GENERATED_CODE_DIR |
| 127 | +``` |
| 128 | + |
| 129 | + |
0 commit comments