Skip to content
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

java headers with dash characters generate non-functional code #68

Closed
RobBlairInq opened this issue Jul 2, 2013 · 3 comments
Closed

Comments

@RobBlairInq
Copy link
Contributor

When the swagger json contains headers with -, the java codegen takes the header name as is, and uses it as a local variable name. This creates non-functional code.
Suggested fix is to have a 'codeParamName' paired with 'paramName', and map - to _

{
  "apiVersion": "1.0",
  "swaggerVersion": "1.2",
  "basePath": "http:/www.sample.com/mapi/v1",
  "resourcePath": "/content/",
  "apis": [ {
      "path": "/content/user-agreement",
      "operations": [ {
          "httpMethod": "GET",
          "summary": "Get localized user-agreement",
          "notes": "User Agreement based on the user's locale.",
          "responseClass": "string",
          "nickname": "userAgreement",
          "produces": ["text/html;charset=UTF-8"],
          "parameters": [ {
              "name": "Accept-Language",
              "description": "ISO code of language accepted",
              "paramType": "header",
              "required": false,
              "allowMultiple": false,
              "dataType": "string"
            }
          ]
        }
      ]
    }
  ],
  "models": {
  }
}

Generated code: note "String Accept-Language" which won't work.

  public String userAgreement (String Accept-Language) throws ApiException {
    // create path and map variables
    String path = "/content/user-agreement".replaceAll("\\{format\\}","json");

    // query params
    Map<String, String> queryParams = new HashMap<String, String>();
    Map<String, String> headerParams = new HashMap<String, String>();

    headerParams.put("Accept-Language", Accept-Language);
    try {
      String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
      if(response != null){
        return (String) ApiInvoker.deserialize(response, "", String.class);
      }
      else {
        return null;
      }
    } catch (ApiException ex) {
      if(ex.getCode() == 404) {
        return null;
      }
      else {
        throw ex;
      }
    }
  }
@fehguy
Copy link
Contributor

fehguy commented Jul 2, 2013

horray, feedback! I believe this is supported in the code, with language-specific transformations. I'll confirm and report back.

@RobBlairInq
Copy link
Contributor Author

Half way there, in BasicJavaGenerator

  override def toVarName(name: String): String = {
    name match {
      case _ if (reservedWords.contains(name)) => escapeReservedWord(name)
      case _ => name.replace("-", "_")
    }
  }

then I think messing with the api.mustache gets the rest of the way - note baseName vs paramName

    {{#headerParams}}headerParams.put("{{baseName}}", {{paramName}});
    {{/headerParams}}

and voila

  public String userAgreement (String Accept_Language) throws ApiException {
    // create path and map variables
    String path = "/content/user-agreement".replaceAll("\\{format\\}","json");

    // query params
    Map<String, String> queryParams = new HashMap<String, String>();
    Map<String, String> headerParams = new HashMap<String, String>();

    headerParams.put("Accept-Language", Accept_Language);

didn't git-branch it well, but could probably do it.

@RobBlairInq
Copy link
Contributor Author

has been fixed, by markac commit 7bde2ff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants