|
| 1 | +package io.swagger.codegen.v3.generators; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import io.swagger.codegen.v3.service.GenerationRequest; |
| 5 | +import io.swagger.codegen.v3.service.GeneratorService; |
| 6 | +import io.swagger.codegen.v3.service.Options; |
| 7 | +import io.swagger.util.Json; |
| 8 | +import io.swagger.util.Yaml; |
| 9 | +import org.apache.commons.io.IOUtils; |
| 10 | +import org.apache.commons.lang3.StringUtils; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + * helper methods to test specific generator result (set of files) |
| 19 | + * |
| 20 | + */ |
| 21 | +public abstract class GeneratorRunner { |
| 22 | + |
| 23 | + public static List<File> runGenerator( |
| 24 | + String name, |
| 25 | + String specPath, |
| 26 | + GenerationRequest.CodegenVersion codegenVersion, |
| 27 | + boolean v2Spec, |
| 28 | + boolean yaml, |
| 29 | + boolean flattenInlineComposedSchema, |
| 30 | + String outFolder |
| 31 | + ) throws Exception { |
| 32 | + |
| 33 | + String path = outFolder; |
| 34 | + if (StringUtils.isBlank(path)) { |
| 35 | + path = getTmpFolder().getAbsolutePath(); |
| 36 | + } |
| 37 | + GenerationRequest request = new GenerationRequest(); |
| 38 | + request |
| 39 | + .codegenVersion(codegenVersion) // use V2 to target Swagger/OpenAPI 2.x Codegen version |
| 40 | + .type(GenerationRequest.Type.CLIENT) |
| 41 | + .lang(name) |
| 42 | + .spec(loadSpecAsNode( specPath, |
| 43 | + yaml, // YAML file, use false for JSON |
| 44 | + v2Spec)) // OpenAPI 3.x - use true for Swagger/OpenAPI 2.x definitions |
| 45 | + .options( |
| 46 | + new Options() |
| 47 | + .flattenInlineComposedSchema(flattenInlineComposedSchema) |
| 48 | + .outputDir(path) |
| 49 | + ); |
| 50 | + |
| 51 | + List<File> files = new GeneratorService().generationRequest(request).generate(); |
| 52 | + return files; |
| 53 | + } |
| 54 | + |
| 55 | + public static File getOutFolder(String path, boolean delete) { |
| 56 | + try { |
| 57 | + File outputFolder = new File(path); |
| 58 | + if (delete) { |
| 59 | + // TODO delete.. |
| 60 | + } |
| 61 | + return outputFolder; |
| 62 | + } catch (Exception e) { |
| 63 | + e.printStackTrace(); |
| 64 | + return null; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static File getTmpFolder() { |
| 69 | + try { |
| 70 | + File outputFolder = File.createTempFile("codegentest-", "-tmp"); |
| 71 | + outputFolder.delete(); |
| 72 | + outputFolder.mkdir(); |
| 73 | + outputFolder.deleteOnExit(); |
| 74 | + return outputFolder; |
| 75 | + } catch (Exception e) { |
| 76 | + e.printStackTrace(); |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static JsonNode loadSpecAsNode(final String file, boolean yaml, boolean v2) { |
| 82 | + InputStream in = null; |
| 83 | + |
| 84 | + try { |
| 85 | + in = GeneratorRunner.class.getClassLoader().getResourceAsStream(file); |
| 86 | + if (yaml) { |
| 87 | + if (v2) { |
| 88 | + return Yaml.mapper().readTree(in); |
| 89 | + } else { |
| 90 | + return io.swagger.v3.core.util.Yaml.mapper().readTree(in); |
| 91 | + } |
| 92 | + } |
| 93 | + if (v2) { |
| 94 | + return Json.mapper().readTree(in); |
| 95 | + } |
| 96 | + return io.swagger.v3.core.util.Json.mapper().readTree(in); |
| 97 | + } catch (Exception e) { |
| 98 | + throw new RuntimeException("could not load file " + file); |
| 99 | + } finally { |
| 100 | + IOUtils.closeQuietly(in); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments