Skip to content

Commit

Permalink
Issue postmanlabs#155 pure java codegen: fix for multiheaders with sa…
Browse files Browse the repository at this point in the history
…me key and delete request failing in travis ci
  • Loading branch information
virenderm01 committed May 30, 2020
1 parent 33cfb9d commit acc4029
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 23 deletions.
3 changes: 2 additions & 1 deletion codegens/java-native/lib/javanative.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function makeSnippet (request, indentString, options) {
}

snippet += parseRequest.parseHeader(request, indentString);

snippet += 'con.setRequestProperty("Accept","application/json");\n';
snippet += `con.setDoOutput(${isBodyRequired ? 'true' : 'false'});\n`;

if (isBodyRequired) {
Expand Down Expand Up @@ -229,6 +229,7 @@ function convert (request, options, callback) {
'import javax.net.ssl.HttpsURLConnection;\n' +
'import java.net.*;\n' +
'import java.lang.reflect.*;\n' +
'import java.util.*;\n' +
'import java.util.function.BiConsumer;\n' +
'public class main {\n' +
indentString + 'public static void main(String []args) throws IOException{\n';
Expand Down
51 changes: 32 additions & 19 deletions codegens/java-native/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,42 +204,55 @@ function parseBody (requestBody, indentString, trimFields) {
return snippet;
}

/**
* Generate java reusable code snippet for adding headers to the request
*
* @param {String} indentString - string for indentation
* @returns {String} - reusable code snippet for adding headers in java
*/
function generateBoilderPlateForHeaders (indentString) {
return 'Map<String, String> headers = new HashMap<>();\n' +
'BiConsumer<String,String> headerMap = (key,value) -> {\n' +
indentString + 'if(headers.get(key)!=null){\n' +
indentString.repeat(2) + 'String val = headers.get(key);\n' +
indentString.repeat(2) + 'val = val + ", "+ value;\n' +
indentString.repeat(2) + 'headers.put(key, val);\n' +
indentString + '}else{\n' +
indentString.repeat(2) + 'headers.put(key,value) ;\n' +
indentString + '}\n' +
'};\n';
}

/**
* Parses header in Postman-SDK request and returns code snippet of java for adding headers
*
* @param {Object} request - Postman SDK request object
* @param {String} indentString - string for indentation
* @returns {String} - code snippet for adding headers in java
*/
function parseHeader (request) {
function parseHeader (request, indentString) {
var headerArray = request.toJSON().header,
headerSnippet = '';

if (!_.isEmpty(headerArray)) {
headerArray = _.reject(headerArray, 'disabled');
headerSnippet += _.reduce(headerArray, function (accumalator, header) {
accumalator += `con.setRequestProperty("${sanitize(header.key, true)}", ` +
if (!_.isEmpty(headerArray)) {
headerSnippet += generateBoilderPlateForHeaders(indentString);
headerSnippet += _.reduce(headerArray, function (accumalator, header) {
accumalator += `headerMap.accept("${sanitize(header.key, true)}", ` +
`"${sanitize(header.value)}");\n`;
return accumalator;
}, '');
return accumalator;
}, '');
headerSnippet += 'for (Map.Entry<String, String> entry : headers.entrySet()) {\n' +
indentString + 'con.setRequestProperty(entry.getKey(), entry.getValue());\n' +
'}';
}
}
return headerSnippet;
}

/**
* returns content-type of request body if available else returns text/plain as default
*
* @param {Object} request - Postman SDK request object
* @returns {String}- content-type of request body
*/
function parseContentType (request) {
if (request.body && request.body.mode === 'graphql') {
return 'application/json';
}
return request.getHeaders({enabled: true, ignoreCase: true})['content-type'] || 'text/plain';
}

module.exports = {
parseBody: parseBody,
parseHeader: parseHeader,
parseContentType: parseContentType
parseHeader: parseHeader
};
77 changes: 74 additions & 3 deletions codegens/java-native/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ describe('java convert function', function () {
return;
}
expect(snippet).to.include('import java.io.*;\nimport javax.net.ssl.HttpsURLConnection;\n' +
'import java.net.*;\nimport java.lang.reflect.*;\nimport java.util.function.BiConsumer;\n' +
'public class main {\n');
'import java.net.*;\nimport java.lang.reflect.*;\nimport java.util.*;\n' +
'import java.util.function.BiConsumer;\npublic class main {\n');
});
});

Expand Down Expand Up @@ -104,7 +104,7 @@ describe('java convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('.setRequestProperty("key_containing_whitespaces", ' +
expect(snippet).to.include('headerMap.accept("key_containing_whitespaces", ' +
'" value_containing_whitespaces ")');
});
});
Expand Down Expand Up @@ -305,6 +305,77 @@ describe('java convert function', function () {
});
});

it('should generate snippets for combining multiple same headers', function () {
var request = new sdk.Request({
'method': 'GET',
'header': [
{
'key': 'key',
'value': 'value1',
'type': 'text'
},
{
'key': 'key',
'value': 'value2',
'type': 'text'
}
],
'url': {
'raw': 'https://postman-echo.com/get',
'protocol': 'https',
'host': [
'postman-echo',
'com'
],
'path': [
'get'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('headerMap.accept("key", "value1")');
expect(snippet).to.include('headerMap.accept("key", "value2")');
expect(snippet).to.include('con.setRequestProperty(entry.getKey(), entry.getValue())');
expect(snippet).to.include('BiConsumer<String,String> headerMap');
expect(snippet).to.include('Map<String, String> headers = new HashMap<>()');
});
});

it('should generate snippets for graph QL body', function () {
var request = new sdk.Request({
'method': 'POST',
'header': [],
'body': {
'mode': 'graphql',
'graphql': {
'query': '{\n body{\n graphql\n }\n}',
'variables': '{\n\t"variable_key": "variable_value"\n}'
}
},
'url': {
'raw': 'https://postman-echo.com/post',
'protocol': 'https',
'host': [
'postman-echo',
'com'
],
'path': [
'post'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('con.setRequestProperty("Content-Type", "application/json")');
});
});

describe('getOptions function', function () {
it('should return array of options for csharp-restsharp converter', function () {
Expand Down

0 comments on commit acc4029

Please sign in to comment.