[Java] Templates to support google-api-client library (#6838)

* Add support for the google-api-client

* When there's no return type, just return (no parsing the response)

* Update the deserialization to use a Jackson typeref.

* Delete the auth mustache files as we don't need them

* Stop generating unneeded auth files; get SBT/gradle working with the generated module

* Add builder-style methods to the google-api-client ApiClient for getting API instances

* Update the README to reflect the new client library option

* Generated overloaded methods to send query params as a Map<String, Object>

* Add files for Sample codes

* Add type in angle brackets to support Java 6

* Required query params must be added to the Map of params. Update sample code.

* Use explicit types instead of diamonds to support Java6 in one more place.

* Clean up javadoc warnings in generated code / remove pointless return statements

* Clean up extra newlines in generated code
This commit is contained in:
Charles Capps
2017-11-03 03:46:07 -07:00
committed by wing328
parent 0ee46c32a2
commit c2eb53ab1d
115 changed files with 12330 additions and 7 deletions

View File

@@ -12,7 +12,7 @@ import java.util.Arrays;
public class CodegenOperation {
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams,
public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams,
returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMapContainer,
isListContainer, isMultipart, hasMore = true,
isResponseBinary = false, isResponseFile = false, hasReference = false,
@@ -28,6 +28,7 @@ public class CodegenOperation {
public List<CodegenParameter> queryParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> formParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> requiredParams = new ArrayList<CodegenParameter>();
public List<CodegenSecurity> authMethods;
public List<Tag> tags;
public List<CodegenResponse> responses = new ArrayList<CodegenResponse>();

View File

@@ -2204,6 +2204,7 @@ public class DefaultCodegen {
List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>();
List<CodegenParameter> cookieParams = new ArrayList<CodegenParameter>();
List<CodegenParameter> formParams = new ArrayList<CodegenParameter>();
List<CodegenParameter> requiredParams = new ArrayList<CodegenParameter>();
if (parameters != null) {
for (Parameter param : parameters) {
@@ -2234,6 +2235,9 @@ public class DefaultCodegen {
allParams.add(p);
if (p.required || p.isBodyParam) {
requiredParams.add(p.copy());
}
// Issue #2561 (neilotoole) : Moved setting of is<Type>Param flags
// from here to fromParameter().
if (param instanceof QueryParameter) {
@@ -2287,6 +2291,7 @@ public class DefaultCodegen {
op.headerParams = addHasMore(headerParams);
// op.cookieParams = cookieParams;
op.formParams = addHasMore(formParams);
op.requiredParams = addHasMore(requiredParams);
op.externalDocs = operation.getExternalDocs();
// legacy support
op.nickname = op.operationId;
@@ -2294,6 +2299,7 @@ public class DefaultCodegen {
if (op.allParams.size() > 0) {
op.hasParams = true;
}
op.hasRequiredParams = op.requiredParams.size() > 0;
// set Restful Flag
op.isRestfulShow = op.isRestfulShow();

View File

@@ -82,6 +82,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
supportedLibraries.put("resttemplate", "HTTP client: Spring RestTemplate 4.3.9-RELEASE. JSON processing: Jackson 2.8.9");
supportedLibraries.put("resteasy", "HTTP client: Resteasy client 3.1.3.Final. JSON processing: Jackson 2.8.9");
supportedLibraries.put("vertx", "HTTP client: VertX client 3.2.4. JSON processing: Jackson 2.8.9");
supportedLibraries.put("google-api-client", "HTTP client: Google API client 1.23.0. JSON processing: Jackson 2.8.9");
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
libraryOption.setEnum(supportedLibraries);
@@ -172,10 +173,13 @@ public class JavaClientCodegen extends AbstractJavaCodegen
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
}
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
// google-api-client doesn't use the Swagger auth, because it uses Google Credential directly (HttpRequestInitializer)
if (!"google-api-client".equals(getLibrary())) {
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
}
supportingFiles.add(new SupportingFile( "gradlew.mustache", "", "gradlew") );
supportingFiles.add(new SupportingFile( "gradlew.bat.mustache", "", "gradlew.bat") );
supportingFiles.add(new SupportingFile( "gradle-wrapper.properties.mustache",
@@ -196,7 +200,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
apiDocTemplateFiles.remove("api_doc.mustache");
}
if (!("feign".equals(getLibrary()) || "resttemplate".equals(getLibrary()) || usesAnyRetrofitLibrary())) {
if (!("feign".equals(getLibrary()) || "resttemplate".equals(getLibrary()) || usesAnyRetrofitLibrary() || "google-api-client".equals(getLibrary()))) {
supportingFiles.add(new SupportingFile("apiException.mustache", invokerFolder, "ApiException.java"));
supportingFiles.add(new SupportingFile("Configuration.mustache", invokerFolder, "Configuration.java"));
supportingFiles.add(new SupportingFile("Pair.mustache", invokerFolder, "Pair.java"));
@@ -240,6 +244,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen
apiTemplateFiles.put("apiImpl.mustache", "Impl.java");
apiTemplateFiles.put("rxApiImpl.mustache", ".java");
supportingFiles.remove(new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
} else if ("google-api-client".equals(getLibrary())) {
additionalProperties.put("jackson", "true");
} else {
LOGGER.error("Unknown library option (-l/--library): " + getLibrary());
}