io.swagger.codegen.DefaultGenerator Java Examples

The following examples show how to use io.swagger.codegen.DefaultGenerator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Meta.java    From TypeScript-Microservices with MIT License 5 votes vote down vote up
/**
 * Converter method to process supporting files: execute with mustache, or simply copy to
 * destination directory
 *
 * @param targetDir - destination directory
 * @param data - map with additional params needed to process templates
 * @return converter object to pass to lambdaj
 */
private static Converter<SupportingFile, File> processFiles(final File targetDir,
        final Map<String, Object> data) {
    return new Converter<SupportingFile, File>() {
        private DefaultGenerator generator = new DefaultGenerator();

        @Override
        public File convert(SupportingFile support) {
            try {
                File destinationFolder =
                        new File(new File(targetDir.getAbsolutePath()), support.folder);
                File outputFile = new File(destinationFolder, support.destinationFilename);

                String template =
                        generator.readTemplate(new File(TEMPLATE_DIR_CLASSPATH,
                                support.templateFile).getPath());
                String formatted = template;

                if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) {
                    LOGGER.info("writing file to {}", outputFile.getAbsolutePath());
                    formatted =
                            Mustache.compiler().withLoader(loader(generator)).defaultValue("")
                                    .compile(template).execute(data);
                } else {
                    LOGGER.info("copying file to {}", outputFile.getAbsolutePath());
                }

                FileUtils.writeStringToFile(outputFile, formatted);
                return outputFile;

            } catch (IOException e) {
                throw new RuntimeException("Can't generate project", e);
            }
        }
    };
}
 
Example #2
Source File: Meta.java    From TypeScript-Microservices with MIT License 5 votes vote down vote up
/**
 * Creates mustache loader for template using classpath loader
 *
 * @param generator - class with reader getter
 * @return loader for template
 */
private static Mustache.TemplateLoader loader(final DefaultGenerator generator) {
    return new Mustache.TemplateLoader() {
        @Override
        public Reader getTemplate(String name) {
            return generator.getTemplateReader(TEMPLATE_DIR_CLASSPATH + File.separator
                    + name.concat(MUSTACHE_EXTENSION));
        }
    };
}
 
Example #3
Source File: IoCCodeGenerator.java    From yang2swagger with Eclipse Public License 1.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        Guice.createInjector(new GeneratorInjector());
        IoCSwaggerGenerator generator;
        if(args.length == 1) {
            // prepare a default generator for a given directory with YANG modules and accept all of them for path
            // generation
            generator = IoCGeneratorHelper.getGenerator(new File(args[0]),m -> true);
        } else {
            // prepare a default generator for all YANG modules and from classpath and accept only these which name
            // starts from 'tapi'
            generator = IoCGeneratorHelper.getGenerator(m -> m.getName().startsWith("Tapi"));
        }
        generator.tagGenerator(new SegmentTagGenerator());
        // -------- configure path generator ---------------
        // for data tree generate only GET operations
//        generator.pathHandler(new PathHandlerBuilder().withoutFullCrud());
        // for data tree generate full CRUD (depending on config flag in yang modules
        Swagger swagger = generator.generate();

        JerseyServerCodegen codegenConfig = new JerseyServerCodegen();

        // referencing handler for x-path annotation
//        codegenConfig.addAnnotation("propAnnotation", "x-path", v ->
//                "@com.mrv.provision.di.rest.jersey.metadata.Leafref(\"" + v + "\")"
//        );

        ClientOpts clientOpts = new ClientOpts();

        Path target = Files.createTempDirectory("generated");
        codegenConfig.additionalProperties().put(CodegenConstants.API_PACKAGE, "com.mrv.provision.di.rest.jersey.tapi.api");
        codegenConfig.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "com.mrv.provision.di.rest.jersey.tapi.model");
        codegenConfig.setOutputDir(target.toString());

        // write swagger.yaml to the target
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.writeValue(new FileWriter(new File(target.toFile(), "tapi.yaml")), swagger);

        ClientOptInput opts = new ClientOptInput().opts(clientOpts).swagger(swagger).config(codegenConfig);
        new DefaultGenerator()
                .opts(opts)
                .generate();
    }