Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.swagger.codegen.languages;

import com.google.common.base.Strings;

import io.cellstore.codegen.CellStoreCodegen;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants;
Expand Down Expand Up @@ -29,7 +31,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
public class JavaClientCodegen extends CellStoreCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);

protected String invokerPackage = "io.swagger.client";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Pattern;

import java.net.URLEncoder;

Expand Down Expand Up @@ -333,11 +334,19 @@ public class ApiClient {
* if JSON exists in the given array, use it;
* otherwise use the first one of the array.
*
* @param headerParams The user provided header parameters map
* @param contentTypes The Content-Type array to select from
* @return The Content-Type header to use. If the given array is empty,
* JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
public String selectHeaderContentType({{javaUtilPrefix}}Map<String, String> headerParams, String[] contentTypes) {
String customContentType = null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange indentation

for (String header: headerParams.keySet())
{
if (header.equalsIgnoreCase("Content-Type"))
customContentType = headerParams.get(header);
}
if (customContentType != null) return customContentType;
if (contentTypes.length == 0) return "application/json";
if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json";
return contentTypes[0];
Expand Down Expand Up @@ -651,4 +660,23 @@ public class ApiClient {
}
return hostMap.get(basePath);
}

/**
* Adds pattern-based parameters (after name validation) to a dictionary query parameters.
* @throws ApiException
*/
public <T> void addPatternQueryParameters(String collectionFormat, Map<String, T> parameters, String pattern, List<Pair> queryParams) throws ApiException
{
Pattern p = Pattern.compile(pattern);
for (Entry<String, T> entry: parameters.entrySet())
{
if (p.matcher(entry.getKey()).matches())
{
queryParams.addAll(parameterToPairs(collectionFormat, entry.getKey(), entry.getValue()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not return the List and do the queryParams.addAll outside of the function? It is not so clear that the queryParams is the "out" parameter. But I leave it to you. Just a suggestion...

}
else
throw new ApiException(400, "Invalid parameter " + entry.getKey() + ", parameter name must match the regular expression \"" + pattern + "\"");
}
}

}
12 changes: 10 additions & 2 deletions modules/swagger-codegen/src/main/resources/Java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class {{classname}} {
}
{{/required}}{{/allParams}}
// create path and map variables
String {{localVariablePrefix}}path = "{{{path}}}".replaceAll("\\{format\\}","json"){{#pathParams}}
String {{localVariablePrefix}}path = "{{{path}}}"{{#pathParams}}
.replaceAll("\\{" + "{{baseName}}" + "\\}", {{localVariablePrefix}}apiClient.escapeString({{{paramName}}}.toString())){{/pathParams}};

// query params
Expand All @@ -62,6 +62,14 @@ public class {{classname}} {
{{#queryParams}}
{{localVariablePrefix}}queryParams.addAll({{localVariablePrefix}}apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
{{/queryParams}}

{{#hardcodedQueryParams}}
{{localVariablePrefix}}queryParams.addAll({{localVariablePrefix}}apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", "{{defaultValue}}"));
{{/hardcodedQueryParams}}

{{#patternQueryParams}}if ({{paramName}} != null)
{{localVariablePrefix}}apiClient.addPatternQueryParameters("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", {{paramName}}, "{{pattern}}", {{localVariablePrefix}}queryParams);
{{/patternQueryParams}}

{{#headerParams}}if ({{paramName}} != null)
{{localVariablePrefix}}headerParams.put("{{baseName}}", {{localVariablePrefix}}apiClient.parameterToString({{paramName}}));
Expand All @@ -79,7 +87,7 @@ public class {{classname}} {
final String[] {{localVariablePrefix}}contentTypes = {
{{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}
};
final String {{localVariablePrefix}}contentType = {{localVariablePrefix}}apiClient.selectHeaderContentType({{localVariablePrefix}}contentTypes);
final String {{localVariablePrefix}}contentType = {{localVariablePrefix}}apiClient.selectHeaderContentType({{localVariablePrefix}}headerParams, {{localVariablePrefix}}contentTypes);

String[] {{localVariablePrefix}}authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}")
@javax.annotation.Generated(value = "{{generatorClass}}")