-
Notifications
You must be signed in to change notification settings - Fork 6k
[Java] Add support of 'x-obfuscated' property allowing for generated model classes an obfuscation of sensitive data (e.g. password) in toString() output (#2662) #7060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e69e0fa
1f97393
758d14c
f7f0d8c
64ca726
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,5 @@ | ||
| #!/bin/sh | ||
| # update java petstore for all supported http libraries | ||
|
|
||
| ./bin/java-petstore-jersey1.sh | ||
| ./bin/java-petstore-jersey2.sh | ||
| ./bin/java-petstore-feign.sh | ||
| ./bin/java-petstore-okhttp-gson.sh | ||
| ./bin/java-petstore-okhttp-gson-parcelable.sh | ||
| ./bin/java-petstore-retrofit.sh | ||
| ./bin/java-petstore-retrofit2.sh | ||
| ./bin/java-petstore-retrofit2rx.sh | ||
| ./bin/java-petstore-retrofit2rx2.sh | ||
| ./bin/java8-petstore-jersey2.sh | ||
| ./bin/java-petstore-retrofit2-play24.sh | ||
| ./bin/java-petstore-jersey2-java6.sh | ||
| ./bin/java-petstore-resttemplate.sh | ||
| ./bin/java-petstore-resttemplate-withxml.sh | ||
| ./bin/java-petstore-resteasy.sh | ||
| ./bin/java-petstore-google-api-client.sh | ||
| ./bin/java-petstore-server-all.sh | ||
| ./bin/java-petstore-client-all.sh | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/bin/sh | ||
| # update java petstore for all supported http client libraries | ||
|
|
||
| ./bin/java-petstore-jersey1.sh | ||
| ./bin/java-petstore-jersey2.sh | ||
| ./bin/java-petstore-feign.sh | ||
| ./bin/java-petstore-okhttp-gson.sh | ||
| ./bin/java-petstore-okhttp-gson-parcelable.sh | ||
| ./bin/java-petstore-retrofit.sh | ||
| ./bin/java-petstore-retrofit2.sh | ||
| ./bin/java-petstore-retrofit2rx.sh | ||
| ./bin/java-petstore-retrofit2rx2.sh | ||
| ./bin/java8-petstore-jersey2.sh | ||
| ./bin/java-petstore-retrofit2-play24.sh | ||
| ./bin/java-petstore-retrofit2-play25.sh | ||
| ./bin/java-petstore-jersey2-java6.sh | ||
| ./bin/java-petstore-resttemplate.sh | ||
| ./bin/java-petstore-resttemplate-withxml.sh | ||
| ./bin/java-petstore-resteasy.sh | ||
| ./bin/java-petstore-google-api-client.sh | ||
| ./bin/java-petstore-vertx.sh |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #!/bin/sh | ||
| # update java petstore for all supported http server libraries | ||
|
|
||
| ./bin/java-inflector-petstore-server.sh | ||
| ./bin/java-msf4j-petstore-server.sh | ||
| ./bin/java-play-framework-petstore-server-all.sh | ||
| ./bin/java-undertow-petstore-server.sh | ||
| ./bin/java-vertx-async-petstore-server.sh | ||
| ./bin/java-vertx-rx-petstore-server.sh | ||
| ./bin/jaxrs-cxf-cdi-petstore-server.sh | ||
| ./bin/jaxrs-cxf-petstore-server.sh | ||
| ./bin/jaxrs-cxf-petstore-server-annotated-base-path.sh | ||
| ./bin/jaxrs-cxf-petstore-server-non-spring-application.sh | ||
| ./bin/jaxrs-jersey1-petstore-server.sh | ||
| ./bin/jaxrs-jersey1-usetags-petstore-server.sh | ||
| ./bin/jaxrs-petstore-server.sh | ||
| ./bin/jaxrs-resteasy-eap-java8-petstore-server.sh | ||
| ./bin/jaxrs-resteasy-eap-joda-petstore-server.sh | ||
| ./bin/jaxrs-resteasy-eap-petstore-server.sh | ||
| ./bin/jaxrs-resteasy-joda-petstore-server.sh | ||
| ./bin/jaxrs-resteasy-petstore-server.sh | ||
| ./bin/jaxrs-spec-petstore-server.sh | ||
| ./bin/jaxrs-spec-petstore-server-interface.sh | ||
| ./bin/jaxrs-usetags-petstore-server.sh | ||
| ./bin/spring-mvc-petstore-j8-async-server.sh | ||
| ./bin/spring-mvc-petstore-server.sh | ||
| ./bin/springboot-petstore-server.sh | ||
| ./bin/springboot-petstore-server-beanvalidation.sh | ||
| ./bin/springboot-petstore-server-implicitHeaders.sh | ||
| ./bin/springboot-petstore-server-useOptional.sh |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #!/bin/sh | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build clients generated by /bin/java-petstore-client-all.sh |
||
| # build client projects generated by java-petstore-client-all.sh | ||
| # run it from swagger-codegen base directory | ||
|
|
||
| red=`tput setaf 1` | ||
| green=`tput setaf 2` | ||
| reset_clr=`tput sgr0` | ||
|
|
||
| declare -a projects=( \ | ||
| feign \ | ||
| google-api-client \ | ||
| jersey1 \ | ||
| jersey2 \ | ||
| jersey2-java6 \ | ||
| jersey2-java8 \ | ||
| okhttp-gson \ | ||
| okhttp-gson-parcelableModel \ | ||
| resteasy \ | ||
| resttemplate \ | ||
| resttemplate-withXml \ | ||
| retrofit \ | ||
| retrofit2 \ | ||
| retrofit2-play24 \ | ||
| retrofit2-play25 \ | ||
| retrofit2rx \ | ||
| retrofit2rx2 \ | ||
| vertx \ | ||
| ) | ||
|
|
||
| echo Build java petstore client projects.. | ||
|
|
||
| base_dir=`pwd` | ||
|
|
||
| for i in "${projects[@]}" | ||
| do | ||
| proj_dir=$base_dir/samples/client/petstore/java/$i | ||
| if [ -d "$proj_dir" ]; then | ||
| cd $proj_dir | ||
| mvn package > ./build.log | ||
| if [ $? = 0 ]; then | ||
| echo " $i - ${green}SUCCEED${reset_clr}" | ||
| else | ||
| echo " $i - ${red}FAILED${reset_clr}" | ||
| fi | ||
| mvn clean &>/dev/null | ||
| else | ||
| echo ${red}project not found:${reset_clr} $proj_dir | ||
| fi | ||
| done | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #!/bin/sh | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build projects generated by /bin/java-petstore-server-all.sh |
||
| # build server projects generated by java-petstore-server-all.sh | ||
| # run it from swagger-codegen base directory | ||
|
|
||
| red=`tput setaf 1` | ||
| green=`tput setaf 2` | ||
| reset_clr=`tput sgr0` | ||
|
|
||
| declare -a projects=( \ | ||
| java-inflector \ | ||
| java-msf4j \ | ||
| java-vertx/async java-vertx/rx \ | ||
| jaxrs/jersey1 jaxrs/jersey1-useTags jaxrs/jersey2 jaxrs/jersey2-useTags \ | ||
| jaxrs-cxf \ | ||
| jaxrs-cxf-annotated-base-path \ | ||
| jaxrs-cxf-cdi \ | ||
| jaxrs-cxf-non-spring-app \ | ||
| jaxrs-resteasy/eap jaxrs-resteasy/eap-java8 jaxrs-resteasy/eap-joda jaxrs-resteasy/joda jaxrs-resteasy/default \ | ||
| jaxrs-spec \ | ||
| jaxrs-spec-interface \ | ||
| spring-mvc \ | ||
| spring-mvc-j8-async \ | ||
| springboot \ | ||
| springboot-beanvalidation \ | ||
| springboot-implicitHeaders \ | ||
| springboot-useoptional \ | ||
| undertow \ | ||
| ) | ||
|
|
||
| echo Build java petstore server projects.. | ||
|
|
||
| base_dir=`pwd` | ||
|
|
||
| for i in "${projects[@]}" | ||
| do | ||
| proj_dir=$base_dir/samples/server/petstore/$i | ||
| if [ -d "$proj_dir" ]; then | ||
| cd $proj_dir | ||
| mvn package > ./build.log | ||
| if [ $? = 0 ]; then | ||
| echo " $i - ${green}SUCCEED${reset_clr}" | ||
| else | ||
| echo " $i - ${red}FAILED${reset_clr}" | ||
| fi | ||
| mvn clean &>/dev/null | ||
| else | ||
| echo ${red}project not found:${reset_clr} $proj_dir | ||
| fi | ||
| done | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package io.swagger.codegen; | ||
|
|
||
| import com.samskivert.mustache.Mustache; | ||
| import com.samskivert.mustache.MustacheException; | ||
| import com.samskivert.mustache.Template; | ||
| import io.swagger.codegen.ignore.CodegenIgnoreProcessor; | ||
| import io.swagger.codegen.languages.AbstractJavaCodegen; | ||
|
|
@@ -19,6 +20,7 @@ | |
|
|
||
| import java.io.*; | ||
| import java.util.*; | ||
| import java.util.regex.Matcher; | ||
|
|
||
| public class DefaultGenerator extends AbstractGenerator implements Generator { | ||
| protected final Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class); | ||
|
|
@@ -257,6 +259,28 @@ private void generateModelTests(List<File> files, Map<String, Object> models, St | |
| } | ||
| } | ||
|
|
||
| /** | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method generates/copies "static" resources for model build. This is similar to "generateSupportingFiles", but "generateSupportingFiles" and "generateModelTests" are independent "phases". |
||
| * Add resource files (either java or any other type) for test model. | ||
| */ | ||
| private void generateModelTestsResourceFiles(List<File> files) { | ||
| Map<String, String> templates = config.modelTestResourceTemplateFiles(); | ||
| if (templates != null && !templates.isEmpty()) { | ||
| Map<String, Object> properties = new HashMap<>(config.additionalProperties()); | ||
| properties.put("package", config.modelPackage()); | ||
| for (String templateName : templates.keySet()) { | ||
| String suffix = templates.get(templateName); | ||
| String templateFile = getFullTemplateFile(config, templateName); | ||
| String outputFileName = config.modelTestFileFolder() + File.separator + config.toModelTestResourceFilename(templateName) + suffix; | ||
| outputFileName = outputFileName.replaceAll("[/|\\\\]", Matcher.quoteReplacement(File.separator)); | ||
| try { | ||
| processTemplateResourceFile(files, properties, templateFile , outputFileName); | ||
| } catch (Exception e) { | ||
| LOGGER.error("Could not generate model test resource file: '" + templateName + "'", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void generateModelDocumentation(List<File> files, Map<String, Object> models, String modelName) throws IOException { | ||
| for (String templateName : config.modelDocTemplateFiles().keySet()) { | ||
| String suffix = config.modelDocTemplateFiles().get(templateName); | ||
|
|
@@ -417,6 +441,11 @@ private Model getParent(Model model) { | |
| throw new RuntimeException("Could not generate model '" + modelName + "'", e); | ||
| } | ||
| } | ||
|
|
||
| if (generateModelTests) { | ||
| generateModelTestsResourceFiles(files); | ||
| } | ||
|
|
||
| if (System.getProperty("debugModels") != null) { | ||
| LOGGER.info("############ Model info ############"); | ||
| Json.prettyPrint(allModels); | ||
|
|
@@ -586,44 +615,7 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl | |
| } | ||
|
|
||
| if (ignoreProcessor.allowsFile(new File(outputFilename))) { | ||
| if (templateFile.endsWith("mustache")) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved into a separate method for reusing. |
||
| String template = readTemplate(templateFile); | ||
| Mustache.Compiler compiler = Mustache.compiler(); | ||
| compiler = config.processCompiler(compiler); | ||
| Template tmpl = compiler | ||
| .withLoader(new Mustache.TemplateLoader() { | ||
| @Override | ||
| public Reader getTemplate(String name) { | ||
| return getTemplateReader(getFullTemplateFile(config, name + ".mustache")); | ||
| } | ||
| }) | ||
| .defaultValue("") | ||
| .compile(template); | ||
|
|
||
| writeToFile(outputFilename, tmpl.execute(bundle)); | ||
| files.add(new File(outputFilename)); | ||
| } else { | ||
| InputStream in = null; | ||
|
|
||
| try { | ||
| in = new FileInputStream(templateFile); | ||
| } catch (Exception e) { | ||
| // continue | ||
| } | ||
| if (in == null) { | ||
| in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile)); | ||
| } | ||
| File outputFile = new File(outputFilename); | ||
| OutputStream out = new FileOutputStream(outputFile, false); | ||
| if (in != null) { | ||
| LOGGER.info("writing file " + outputFile); | ||
| IOUtils.copy(in, out); | ||
| out.close(); | ||
| } else { | ||
| LOGGER.error("can't open " + templateFile + " for input"); | ||
| } | ||
| files.add(outputFile); | ||
| } | ||
| processTemplateResourceFile(files, bundle, templateFile, outputFilename); | ||
| } else { | ||
| LOGGER.info("Skipped generation of " + outputFilename + " due to rule in .swagger-codegen-ignore"); | ||
| } | ||
|
|
@@ -679,6 +671,47 @@ public Reader getTemplate(String name) { | |
|
|
||
| } | ||
|
|
||
| private void processTemplateResourceFile(List<File> files, Map<String, Object> bundle, String templateFile, String outputFilename) throws MustacheException, IOException { | ||
| if (templateFile.endsWith("mustache")) { | ||
| String template = readTemplate(templateFile); | ||
| Mustache.Compiler compiler = Mustache.compiler(); | ||
| compiler = config.processCompiler(compiler); | ||
| Template tmpl = compiler | ||
| .withLoader(new Mustache.TemplateLoader() { | ||
| @Override | ||
| public Reader getTemplate(String name) { | ||
| return getTemplateReader(getFullTemplateFile(config, name + ".mustache")); | ||
| } | ||
| }) | ||
| .defaultValue("") | ||
| .compile(template); | ||
|
|
||
| writeToFile(outputFilename, tmpl.execute(bundle)); | ||
| files.add(new File(outputFilename)); | ||
| } else { | ||
| InputStream in = null; | ||
|
|
||
| try { | ||
| in = new FileInputStream(templateFile); | ||
| } catch (Exception e) { | ||
| // continue | ||
| } | ||
| if (in == null) { | ||
| in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile)); | ||
| } | ||
| File outputFile = new File(outputFilename); | ||
| OutputStream out = new FileOutputStream(outputFile, false); | ||
| if (in != null) { | ||
| LOGGER.info("writing file " + outputFile); | ||
| IOUtils.copy(in, out); | ||
| out.close(); | ||
| } else { | ||
| LOGGER.error("can't open " + templateFile + " for input"); | ||
| } | ||
| files.add(outputFile); | ||
| } | ||
| } | ||
|
|
||
| private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) { | ||
|
|
||
| Map<String, Object> bundle = new HashMap<String, Object>(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved these scripts to java-petstore-client-all.sh (all of them are clients).
Also added there retrofit2-play25 and vertx clients.