Skip to content

Commit

Permalink
fix NPE when host is undefined (#7633)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Feb 10, 2018
1 parent 0a28aad commit 854b501
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig {

Expand Down Expand Up @@ -154,7 +155,7 @@ public String toApiFilename(String name) {
public String apiFilename(String templateName, String tag) {
String result = super.apiFilename(templateName, tag);

if ( templateName.equals("service.mustache") ) {
if (templateName.equals("service.mustache")) {
String stringToMatch = File.separator + "controllers" + File.separator;
String replacement = File.separator + implFolder + File.separator;
result = result.replaceAll(Pattern.quote(stringToMatch), replacement);
Expand All @@ -173,7 +174,7 @@ private String implFileFolder(String output) {
* @return the escaped term
*/
@Override
public String escapeReservedWord(String name) {
public String escapeReservedWord(String name) {
if(this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
Expand Down Expand Up @@ -318,12 +319,18 @@ public void processOpts() {
public void preprocessSwagger(Swagger swagger) {
String host = swagger.getHost();
String port = "8080";
if (host != null) {

if (!StringUtils.isEmpty(host)) {
String[] parts = host.split(":");
if (parts.length > 1) {
port = parts[1];
}
} else {
// host is empty, default to https://localhost
host = "http://localhost";
LOGGER.warn("'host' in the specification is empty or undefined. Default to http://localhost.");
}

this.additionalProperties.put("serverPort", port);

if (swagger.getInfo() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: Apache-2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: petstore.swagger.io
host: petstore.swagger.io
basePath: /v2
tags:
- name: pet
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3-SNAPSHOT
2.4.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ info:
contact:
email: "apiteam@swagger.io"
license:
name: "Apache 2.0"
name: "Apache-2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
tags:
- name: "pet"
Expand Down Expand Up @@ -108,11 +107,11 @@ paths:
type: "array"
items:
type: "string"
default: "available"
enum:
- "available"
- "pending"
- "sold"
default: "available"
collectionFormat: "csv"
responses:
200:
Expand Down Expand Up @@ -161,6 +160,7 @@ paths:
- petstore_auth:
- "write:pets"
- "read:pets"
deprecated: true
x-swagger-router-controller: "Pet"
/pet/{petId}:
get:
Expand Down Expand Up @@ -625,6 +625,13 @@ definitions:
default: false
title: "Pet Order"
description: "An order for a pets from the pet store"
example:
petId: 6
quantity: 1
id: 0
shipDate: "2000-01-23T04:56:07.000+00:00"
complete: false
status: "placed"
xml:
name: "Order"
Category:
Expand All @@ -637,6 +644,9 @@ definitions:
type: "string"
title: "Pet category"
description: "A category for a pet"
example:
name: "name"
id: 6
xml:
name: "Category"
User:
Expand All @@ -663,6 +673,15 @@ definitions:
description: "User Status"
title: "a User"
description: "A User who is purchasing from the pet store"
example:
firstName: "firstName"
lastName: "lastName"
password: "password"
userStatus: 6
phone: "phone"
id: 0
email: "email"
username: "username"
xml:
name: "User"
Tag:
Expand All @@ -675,6 +694,9 @@ definitions:
type: "string"
title: "Pet Tag"
description: "A tag for a pet"
example:
name: "name"
id: 1
xml:
name: "Tag"
Pet:
Expand Down Expand Up @@ -714,6 +736,21 @@ definitions:
- "sold"
title: "a Pet"
description: "A pet for sale in the pet store"
example:
photoUrls:
- "photoUrls"
- "photoUrls"
name: "doggie"
id: 0
category:
name: "name"
id: 6
tags:
- name: "name"
id: 1
- name: "name"
id: 1
status: "available"
xml:
name: "Pet"
ApiResponse:
Expand All @@ -728,6 +765,10 @@ definitions:
type: "string"
title: "An uploaded response"
description: "Describes the result of uploading an image resource"
example:
code: 0
type: "type"
message: "message"
externalDocs:
description: "Find out more about Swagger"
url: "http://swagger.io"
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,34 @@ exports.findPetsByStatus = function(status) {
return new Promise(function(resolve, reject) {
var examples = {};
examples['application/json'] = [ {
"photoUrls" : [ "aeiou" ],
"photoUrls" : [ "photoUrls", "photoUrls" ],
"name" : "doggie",
"id" : 0,
"category" : {
"name" : "aeiou",
"name" : "name",
"id" : 6
},
"tags" : [ {
"name" : "aeiou",
"name" : "name",
"id" : 1
}, {
"name" : "name",
"id" : 1
} ],
"status" : "available"
}, {
"photoUrls" : [ "photoUrls", "photoUrls" ],
"name" : "doggie",
"id" : 0,
"category" : {
"name" : "name",
"id" : 6
},
"tags" : [ {
"name" : "name",
"id" : 1
}, {
"name" : "name",
"id" : 1
} ],
"status" : "available"
Expand All @@ -74,15 +93,34 @@ exports.findPetsByTags = function(tags) {
return new Promise(function(resolve, reject) {
var examples = {};
examples['application/json'] = [ {
"photoUrls" : [ "aeiou" ],
"photoUrls" : [ "photoUrls", "photoUrls" ],
"name" : "doggie",
"id" : 0,
"category" : {
"name" : "aeiou",
"name" : "name",
"id" : 6
},
"tags" : [ {
"name" : "aeiou",
"name" : "name",
"id" : 1
}, {
"name" : "name",
"id" : 1
} ],
"status" : "available"
}, {
"photoUrls" : [ "photoUrls", "photoUrls" ],
"name" : "doggie",
"id" : 0,
"category" : {
"name" : "name",
"id" : 6
},
"tags" : [ {
"name" : "name",
"id" : 1
}, {
"name" : "name",
"id" : 1
} ],
"status" : "available"
Expand All @@ -107,15 +145,18 @@ exports.getPetById = function(petId) {
return new Promise(function(resolve, reject) {
var examples = {};
examples['application/json'] = {
"photoUrls" : [ "aeiou" ],
"photoUrls" : [ "photoUrls", "photoUrls" ],
"name" : "doggie",
"id" : 0,
"category" : {
"name" : "aeiou",
"name" : "name",
"id" : 6
},
"tags" : [ {
"name" : "aeiou",
"name" : "name",
"id" : 1
}, {
"name" : "name",
"id" : 1
} ],
"status" : "available"
Expand Down Expand Up @@ -173,8 +214,8 @@ exports.uploadFile = function(petId,additionalMetadata,file) {
var examples = {};
examples['application/json'] = {
"code" : 0,
"type" : "aeiou",
"message" : "aeiou"
"type" : "type",
"message" : "message"
};
if (Object.keys(examples).length > 0) {
resolve(examples[Object.keys(examples)[0]]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ exports.getUserByName = function(username) {
return new Promise(function(resolve, reject) {
var examples = {};
examples['application/json'] = {
"firstName" : "aeiou",
"lastName" : "aeiou",
"password" : "aeiou",
"firstName" : "firstName",
"lastName" : "lastName",
"password" : "password",
"userStatus" : 6,
"phone" : "aeiou",
"phone" : "phone",
"id" : 0,
"email" : "aeiou",
"username" : "aeiou"
"email" : "email",
"username" : "username"
};
if (Object.keys(examples).length > 0) {
resolve(examples[Object.keys(examples)[0]]);
Expand All @@ -97,7 +97,7 @@ exports.getUserByName = function(username) {
exports.loginUser = function(username,password) {
return new Promise(function(resolve, reject) {
var examples = {};
examples['application/json'] = "aeiou";
examples['application/json'] = "";
if (Object.keys(examples).length > 0) {
resolve(examples[Object.keys(examples)[0]]);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ var writeJson = exports.writeJson = function(response, arg1, arg2) {
payload = JSON.stringify(payload, null, 2);
}
response.writeHead(code, {'Content-Type': 'application/json'});
response.end(payload, code);
}
response.end(payload);
}

0 comments on commit 854b501

Please sign in to comment.