Skip to content

Commit

Permalink
Add lua test script, minor fix to Lua API files (#7490)
Browse files Browse the repository at this point in the history
* add lua test script, minor fix to api files

* fix model import, avoid null header issue

* add space before equal sign
  • Loading branch information
wing328 committed Jan 25, 2018
1 parent e33b350 commit df10c72
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 73 deletions.
4 changes: 2 additions & 2 deletions bin/lua-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ then
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/swagger-codegen/src/main/resources/lua -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l lua -o samples/client/petstore/lua -DpackageName=petstore $@"
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties $@"
ags="generate -t modules/swagger-codegen/src/main/resources/lua -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l lua -o samples/client/petstore/lua -DpackageName=petstore"

java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,9 @@ private Map<String, Object> processOperations(CodegenConfig config, String tag,
}
if (mapping != null) {
im.put("import", mapping);
imports.add(im);
if (!imports.contains(im)) { // avoid duplicates
imports.add(im);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public LuaClientCodegen() {
typeMapping.put("date", "string");
typeMapping.put("DateTime", "string");
typeMapping.put("password", "string");
typeMapping.put("file", "TODO_FILE_MAPPING");
// TODO fix file mapping
typeMapping.put("file", "string");
// map binary to string as a workaround
// the correct solution is to use []byte
typeMapping.put("binary", "string");
Expand Down Expand Up @@ -181,11 +182,11 @@ public String escapeReservedWord(String name)

@Override
public String apiFileFolder() {
return outputFolder + File.separator + "api" + File.separator;
return outputFolder + File.separator + packageName + File.separator + "api" + File.separator;
}

public String modelFileFolder() {
return outputFolder + File.separator + "model" + File.separator;
return outputFolder + File.separator + packageName + File.separator + "model" + File.separator;
}

@Override
Expand Down Expand Up @@ -519,4 +520,13 @@ public String toEnumName(CodegenProperty property) {
return enumName;
}
}

@Override
public String toModelImport(String name) {
if (needToImport(toModelName(name))) {
return toModelName(name);
}

return name;
}
}
32 changes: 22 additions & 10 deletions modules/swagger-codegen/src/main/resources/lua/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ local dkjson = require "dkjson"
local basexx = require "basexx"

-- model import
local {{{packageName}}}_{{classname}} = require "{{{packageName}}}.api.{{classname}}"
{{#imports}}
local {{{packageName}}}_{{import}} = require "{{{packageName}}}.model.{{import}}"
{{/imports}}

local {{{packageName}}}= {}
local {{{packageName}}}_mt = {
local {{{classname}}} = {}
local {{{classname}}}_mt = {
__name = "{{{classname}}}";
__index = {{{packageName}}};
__index = {{{classname}}};
}

local function new_{{classname}}(host, basePath, schemes)
Expand All @@ -31,7 +33,7 @@ local function new_{{classname}}(host, basePath, schemes)
http_password = nil;
api_key = {};
access_token = nil;
}, {{{packageName}}}_mt)
}, {{{classname}}}_mt)
end

{{#operation}}
Expand Down Expand Up @@ -60,7 +62,9 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},

{{/hasProduces}}
{{#headerParams}}
req.headers:upsert("{{baseName}}", {{paramName}})
if {{paramName}} then
req.headers:upsert("{{baseName}}", {{paramName}})
end
{{/headerParams}}
{{#formParams}}
{{#-first}}
Expand All @@ -79,7 +83,9 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
{{#isApiKey}}
{{#isKeyInHeader}}
-- api key in headers '{{keyParamName}}'
req.headers:upsert("{{{name}}}", api_key['{{{keyParamName}}}'])
if self.api_key['{{{keyParamName}}}'] then
req.headers:upsert("{{{name}}}", self.api_key['{{{keyParamName}}}'])
end
{{/isKeyInHeader}}
{{#isKeyInQuery}}
-- TODO: api key in query '{{keyParamName}}'
Expand All @@ -91,7 +97,9 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
{{/isBasic}}
{{#isOAuth}}
-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end
{{/isOAuth}}
{{/authMethods}}

Expand Down Expand Up @@ -120,7 +128,7 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
{{^returnTypeIsPrimitive}}
{{#isListContainer}}
for _, ob in ipairs(result) do
cast_{{returnType}}(ob)
{{packageName}}_{{returnType}}.cast(ob)
end
return result, headers
{{/isListContainer}}
Expand All @@ -129,7 +137,7 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
{{/isMapContainer}}
{{^isMapContainer}}
{{^isListContainer}}
return cast_{{returnType}}(result), headers
return {{packageName}}_{{returnType}}.cast(result), headers
{{/isListContainer}}
{{/isMapContainer}}
{{/returnTypeIsPrimitive}}
Expand All @@ -149,4 +157,8 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
end

{{/operation}}
return {
new = new_{{classname}};
}
{{/operations}}

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1-SNAPSHOT
2.4.0-SNAPSHOT

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class Animal {
@SerializedName("color")
private String color = "red";

public Animal() {
this.className = this.getClass().getSimpleName();
}
public Animal className(String className) {
this.className = className;
return this;
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/lua/.swagger-codegen/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0-SNAPSHOT
2.4.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/lua/git_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ git_remote=`git remote`
if [ "$git_remote" = "" ]; then # git remote not defined

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment."
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ local dkjson = require "dkjson"
local basexx = require "basexx"

-- model import
local petstore_pet_api = require "petstore.api.pet_api"
local petstore_api_response = require "petstore.model.api_response"
local petstore_pet = require "petstore.model.pet"

local petstore= {}
local petstore_mt = {
local pet_api = {}
local pet_api_mt = {
__name = "pet_api";
__index = petstore;
__index = pet_api;
}

local function new_pet_api(host, basePath, schemes)
Expand All @@ -39,7 +40,7 @@ local function new_pet_api(host, basePath, schemes)
http_password = nil;
api_key = {};
access_token = nil;
}, petstore_mt)
}, pet_api_mt)
end

function pet_api:add_pet(body)
Expand All @@ -65,7 +66,9 @@ function pet_api:add_pet(body)
req:set_body(dkjson.encode(body))

-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand Down Expand Up @@ -101,9 +104,13 @@ function pet_api:delete_pet(pet_id, api_key)
--local var_accept = { "application/xml", "application/json" }
req.headers:upsert("content-type", "application/xml")

req.headers:upsert("api_key", api_key)
if api_key then
req.headers:upsert("api_key", api_key)
end
-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand Down Expand Up @@ -140,7 +147,9 @@ function pet_api:find_pets_by_status(status)
req.headers:upsert("content-type", "application/xml")

-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand All @@ -161,7 +170,7 @@ function pet_api:find_pets_by_status(status)
return nil, err3
end
for _, ob in ipairs(result) do
cast_pet(ob)
petstore_pet.cast(ob)
end
return result, headers
else
Expand Down Expand Up @@ -191,7 +200,9 @@ function pet_api:find_pets_by_tags(tags)
req.headers:upsert("content-type", "application/xml")

-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand All @@ -212,7 +223,7 @@ function pet_api:find_pets_by_tags(tags)
return nil, err3
end
for _, ob in ipairs(result) do
cast_pet(ob)
petstore_pet.cast(ob)
end
return result, headers
else
Expand Down Expand Up @@ -242,7 +253,9 @@ function pet_api:get_pet_by_id(pet_id)
req.headers:upsert("content-type", "application/xml")

-- api key in headers 'api_key'
req.headers:upsert("api_key", api_key['api_key'])
if self.api_key['api_key'] then
req.headers:upsert("api_key", self.api_key['api_key'])
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand All @@ -262,7 +275,7 @@ function pet_api:get_pet_by_id(pet_id)
if result == nil then
return nil, err3
end
return cast_pet(result), headers
return petstore_pet.cast(result), headers
else
local body, err, errno2 = stream:get_body_as_string()
if not body then
Expand Down Expand Up @@ -297,7 +310,9 @@ function pet_api:update_pet(body)
req:set_body(dkjson.encode(body))

-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand Down Expand Up @@ -343,7 +358,9 @@ function pet_api:update_pet_with_form(pet_id, name, status)
["status"] = status;
}))
-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand Down Expand Up @@ -389,7 +406,9 @@ function pet_api:upload_file(pet_id, additional_metadata, file)
["file"] = file;
}))
-- oAuth
req.headers:upsert("authorization", "Bearer " .. self.access_token)
if self.access_token then
req.headers:upsert("authorization", "Bearer " .. self.access_token)
end

-- make the HTTP call
local headers, stream, errno = req:go()
Expand All @@ -409,7 +428,7 @@ function pet_api:upload_file(pet_id, additional_metadata, file)
if result == nil then
return nil, err3
end
return cast_api_response(result), headers
return petstore_api_response.cast(result), headers
else
local body, err, errno2 = stream:get_body_as_string()
if not body then
Expand All @@ -421,3 +440,7 @@ function pet_api:upload_file(pet_id, additional_metadata, file)
end
end

return {
new = new_pet_api;
}

0 comments on commit df10c72

Please sign in to comment.