forked from loafle/openapi-generator-original
Add lua test script, minor fix to Lua API files (#7490)
* add lua test script, minor fix to api files * fix model import, avoid null header issue * add space before equal sign
This commit is contained in:
parent
e33b350c89
commit
df10c725ab
@ -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
|
||||
|
@ -977,9 +977,11 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
}
|
||||
if (mapping != null) {
|
||||
im.put("import", mapping);
|
||||
if (!imports.contains(im)) { // avoid duplicates
|
||||
imports.add(im);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
operations.put("imports", imports);
|
||||
|
||||
|
@ -89,7 +89,8 @@ public class LuaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
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");
|
||||
@ -181,11 +182,11 @@ public class LuaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@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
|
||||
@ -519,4 +520,13 @@ public class LuaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
return enumName;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelImport(String name) {
|
||||
if (needToImport(toModelName(name))) {
|
||||
return toModelName(name);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
@ -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}}
|
||||
@ -60,7 +62,9 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
|
||||
|
||||
{{/hasProduces}}
|
||||
{{#headerParams}}
|
||||
if {{paramName}} then
|
||||
req.headers:upsert("{{baseName}}", {{paramName}})
|
||||
end
|
||||
{{/headerParams}}
|
||||
{{#formParams}}
|
||||
{{#-first}}
|
||||
@ -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}}'
|
||||
@ -91,7 +97,9 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
|
||||
{{/isBasic}}
|
||||
{{#isOAuth}}
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
|
||||
@ -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}}
|
||||
@ -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}}
|
||||
@ -149,4 +157,8 @@ function {{classname}}:{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}},
|
||||
end
|
||||
|
||||
{{/operation}}
|
||||
return {
|
||||
new = new_{{classname}};
|
||||
}
|
||||
{{/operations}}
|
||||
|
||||
|
@ -1 +1 @@
|
||||
2.3.1-SNAPSHOT
|
||||
2.4.0-SNAPSHOT
|
@ -1,8 +0,0 @@
|
||||
/*___Generated_by_IDEA___*/
|
||||
|
||||
package io.swagger.client;
|
||||
|
||||
/* This stub is only used by the IDE. It is NOT the BuildConfig class actually packed into the APK */
|
||||
public final class BuildConfig {
|
||||
public final static boolean DEBUG = Boolean.parseBoolean(null);
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
/*___Generated_by_IDEA___*/
|
||||
|
||||
package io.swagger.client;
|
||||
|
||||
/* This stub is only used by the IDE. It is NOT the Manifest class actually packed into the APK */
|
||||
public final class Manifest {
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
/*___Generated_by_IDEA___*/
|
||||
|
||||
package io.swagger.client;
|
||||
|
||||
/* This stub is only used by the IDE. It is NOT the R class actually packed into the APK */
|
||||
public final class R {
|
||||
}
|
@ -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;
|
||||
|
@ -1 +1 @@
|
||||
2.3.0-SNAPSHOT
|
||||
2.4.0-SNAPSHOT
|
@ -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
|
||||
|
@ -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)
|
||||
@ -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)
|
||||
@ -65,7 +66,9 @@ function pet_api:add_pet(body)
|
||||
req:set_body(dkjson.encode(body))
|
||||
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
|
||||
-- make the HTTP call
|
||||
local headers, stream, errno = req:go()
|
||||
@ -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")
|
||||
|
||||
if api_key then
|
||||
req.headers:upsert("api_key", api_key)
|
||||
end
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
|
||||
-- make the HTTP call
|
||||
local headers, stream, errno = req:go()
|
||||
@ -140,7 +147,9 @@ function pet_api:find_pets_by_status(status)
|
||||
req.headers:upsert("content-type", "application/xml")
|
||||
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
|
||||
-- make the HTTP call
|
||||
local headers, stream, errno = req:go()
|
||||
@ -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
|
||||
@ -191,7 +200,9 @@ function pet_api:find_pets_by_tags(tags)
|
||||
req.headers:upsert("content-type", "application/xml")
|
||||
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
|
||||
-- make the HTTP call
|
||||
local headers, stream, errno = req:go()
|
||||
@ -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
|
||||
@ -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()
|
||||
@ -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
|
||||
@ -297,7 +310,9 @@ function pet_api:update_pet(body)
|
||||
req:set_body(dkjson.encode(body))
|
||||
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
|
||||
-- make the HTTP call
|
||||
local headers, stream, errno = req:go()
|
||||
@ -343,7 +358,9 @@ function pet_api:update_pet_with_form(pet_id, name, status)
|
||||
["status"] = status;
|
||||
}))
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
|
||||
-- make the HTTP call
|
||||
local headers, stream, errno = req:go()
|
||||
@ -389,7 +406,9 @@ function pet_api:upload_file(pet_id, additional_metadata, file)
|
||||
["file"] = file;
|
||||
}))
|
||||
-- oAuth
|
||||
if self.access_token then
|
||||
req.headers:upsert("authorization", "Bearer " .. self.access_token)
|
||||
end
|
||||
|
||||
-- make the HTTP call
|
||||
local headers, stream, errno = req:go()
|
||||
@ -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
|
||||
@ -421,3 +440,7 @@ function pet_api:upload_file(pet_id, additional_metadata, file)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
new = new_pet_api;
|
||||
}
|
||||
|
@ -16,12 +16,12 @@ local dkjson = require "dkjson"
|
||||
local basexx = require "basexx"
|
||||
|
||||
-- model import
|
||||
local petstore_store_api = require "petstore.api.store_api"
|
||||
local petstore_order = require "petstore.model.order"
|
||||
|
||||
local petstore= {}
|
||||
local petstore_mt = {
|
||||
local store_api = {}
|
||||
local store_api_mt = {
|
||||
__name = "store_api";
|
||||
__index = petstore;
|
||||
__index = store_api;
|
||||
}
|
||||
|
||||
local function new_store_api(host, basePath, schemes)
|
||||
@ -39,7 +39,7 @@ local function new_store_api(host, basePath, schemes)
|
||||
http_password = nil;
|
||||
api_key = {};
|
||||
access_token = nil;
|
||||
}, petstore_mt)
|
||||
}, store_api_mt)
|
||||
end
|
||||
|
||||
function store_api:delete_order(order_id)
|
||||
@ -93,7 +93,9 @@ function store_api:get_inventory()
|
||||
req.headers:upsert("content-type", "application/json")
|
||||
|
||||
-- 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()
|
||||
@ -159,7 +161,7 @@ function store_api:get_order_by_id(order_id)
|
||||
if result == nil then
|
||||
return nil, err3
|
||||
end
|
||||
return cast_order(result), headers
|
||||
return petstore_order.cast(result), headers
|
||||
else
|
||||
local body, err, errno2 = stream:get_body_as_string()
|
||||
if not body then
|
||||
@ -207,7 +209,7 @@ function store_api:place_order(body)
|
||||
if result == nil then
|
||||
return nil, err3
|
||||
end
|
||||
return cast_order(result), headers
|
||||
return petstore_order.cast(result), headers
|
||||
else
|
||||
local body, err, errno2 = stream:get_body_as_string()
|
||||
if not body then
|
||||
@ -219,3 +221,7 @@ function store_api:place_order(body)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
new = new_store_api;
|
||||
}
|
||||
|
@ -16,12 +16,12 @@ local dkjson = require "dkjson"
|
||||
local basexx = require "basexx"
|
||||
|
||||
-- model import
|
||||
local petstore_user_api = require "petstore.api.user_api"
|
||||
local petstore_user = require "petstore.model.user"
|
||||
|
||||
local petstore= {}
|
||||
local petstore_mt = {
|
||||
local user_api = {}
|
||||
local user_api_mt = {
|
||||
__name = "user_api";
|
||||
__index = petstore;
|
||||
__index = user_api;
|
||||
}
|
||||
|
||||
local function new_user_api(host, basePath, schemes)
|
||||
@ -39,7 +39,7 @@ local function new_user_api(host, basePath, schemes)
|
||||
http_password = nil;
|
||||
api_key = {};
|
||||
access_token = nil;
|
||||
}, petstore_mt)
|
||||
}, user_api_mt)
|
||||
end
|
||||
|
||||
function user_api:create_user(body)
|
||||
@ -222,7 +222,7 @@ function user_api:get_user_by_name(username)
|
||||
if result == nil then
|
||||
return nil, err3
|
||||
end
|
||||
return cast_user(result), headers
|
||||
return petstore_user.cast(result), headers
|
||||
else
|
||||
local body, err, errno2 = stream:get_body_as_string()
|
||||
if not body then
|
||||
@ -352,3 +352,7 @@ function user_api:update_user(username, body)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
new = new_user_api;
|
||||
}
|
||||
|
12
samples/client/petstore/lua/test.lua
Normal file
12
samples/client/petstore/lua/test.lua
Normal file
@ -0,0 +1,12 @@
|
||||
local petstore_client = require "petstore.api.pet_api"
|
||||
local petstore_client_pet = require "petstore.model.pet"
|
||||
|
||||
local my_pet_http_api = petstore_client.new("petstore.swagger.io", "/v2", {"http"})
|
||||
|
||||
local my_pet = my_pet_http_api:get_pet_by_id(5)
|
||||
for k,v in pairs(my_pet) do
|
||||
print(k,v)
|
||||
end
|
||||
|
||||
local my_new_pet = petstore_client_pet.new("Mr. Barks")
|
||||
my_pet_http_api:add_pet(my_new_pet)
|
Loading…
x
Reference in New Issue
Block a user