From d5304055ab2dc9eeda038f3b48b9d60a104d521f Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 9 May 2020 14:26:54 +0800 Subject: [PATCH] Minor improvements to the Lua client generator (#6219) * minor improvement to lua client gen * add new files --- bin/lua-petstore.sh | 2 +- bin/openapi3/lua-petstore.sh | 32 ----------------- bin/utils/ensure-up-to-date | 1 + bin/windows/lua-petstore.bat | 2 +- docs/generators.md | 2 +- .../codegen/languages/LuaClientCodegen.java | 6 ++++ .../petstore/lua/.openapi-generator/VERSION | 2 +- samples/client/petstore/lua/git_push.sh | 14 +++++--- .../petstore/lua/petstore-1.0.0-1.rockspec | 2 ++ .../petstore/lua/petstore/api/pet_api.lua | 36 +++++++++++++++++-- .../petstore/lua/petstore/api/store_api.lua | 6 +++- .../petstore/lua/petstore/api/user_api.lua | 18 +++++++++- .../lua/petstore/model/api_response.lua | 2 +- .../petstore/lua/petstore/model/category.lua | 2 +- .../lua/petstore/model/inline_object.lua | 32 +++++++++++++++++ .../lua/petstore/model/inline_object_1.lua | 32 +++++++++++++++++ .../petstore/lua/petstore/model/order.lua | 2 +- .../petstore/lua/petstore/model/pet.lua | 2 +- .../petstore/lua/petstore/model/tag.lua | 2 +- .../petstore/lua/petstore/model/user.lua | 2 +- .../lua/spec/inline_object_1_spec.lua | 33 +++++++++++++++++ .../petstore/lua/spec/inline_object_spec.lua | 33 +++++++++++++++++ 22 files changed, 214 insertions(+), 51 deletions(-) delete mode 100755 bin/openapi3/lua-petstore.sh create mode 100644 samples/client/petstore/lua/petstore/model/inline_object.lua create mode 100644 samples/client/petstore/lua/petstore/model/inline_object_1.lua create mode 100644 samples/client/petstore/lua/spec/inline_object_1_spec.lua create mode 100644 samples/client/petstore/lua/spec/inline_object_spec.lua diff --git a/bin/lua-petstore.sh b/bin/lua-petstore.sh index 44b5cc38637..77e9589e85d 100755 --- a/bin/lua-petstore.sh +++ b/bin/lua-petstore.sh @@ -27,6 +27,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties $@" -ags="generate -t modules/openapi-generator/src/main/resources/lua -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g lua -o samples/client/petstore/lua --additional-properties packageName=petstore $@" +ags="generate -t modules/openapi-generator/src/main/resources/lua -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g lua -o samples/client/petstore/lua --additional-properties packageName=petstore $@" java $JAVA_OPTS -jar $executable $ags diff --git a/bin/openapi3/lua-petstore.sh b/bin/openapi3/lua-petstore.sh deleted file mode 100755 index f598c67aa02..00000000000 --- a/bin/openapi3/lua-petstore.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/sh - -SCRIPT="$0" -echo "# START SCRIPT: $SCRIPT" - -while [ -h "$SCRIPT" ] ; do - ls=`ls -ld "$SCRIPT"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - SCRIPT="$link" - else - SCRIPT=`dirname "$SCRIPT"`/"$link" - fi -done - -if [ ! -d "${APP_DIR}" ]; then - APP_DIR=`dirname "$SCRIPT"`/.. - APP_DIR=`cd "${APP_DIR}"; pwd` -fi - -executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar" - -if [ ! -f "$executable" ] -then - mvn clean package -fi - -# if you've executed sbt assembly previously it will use that instead. -export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/lua -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g lua -o samples/client/petstore/lua --additional-properties packageName=petstore $@" - -java $JAVA_OPTS -jar $executable $ags diff --git a/bin/utils/ensure-up-to-date b/bin/utils/ensure-up-to-date index 03a2823557b..ff9425a3a62 100755 --- a/bin/utils/ensure-up-to-date +++ b/bin/utils/ensure-up-to-date @@ -88,6 +88,7 @@ declare -a samples=( "${root}/bin/scala-akka-petstore.sh" "${root}/bin/openapi3/scala-akka-petstore.sh" "${root}/bin/openapi3/scala-sttp-petstore.sh" +"${root}/bin/lua-petstore.sh" ) # Some special case generators may expect to be run as a stanalone process (e.g. modifying classpath) diff --git a/bin/windows/lua-petstore.bat b/bin/windows/lua-petstore.bat index db41d48b651..fe06bd229a5 100755 --- a/bin/windows/lua-petstore.bat +++ b/bin/windows/lua-petstore.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties -set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g lua -o samples\client\petstore\lua +set ags=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g lua -o samples\client\petstore\lua java %JAVA_OPTS% -jar %executable% %ags% diff --git a/docs/generators.md b/docs/generators.md index 6d7800fd694..4e63bd89256 100644 --- a/docs/generators.md +++ b/docs/generators.md @@ -40,7 +40,7 @@ The following generators are available: * [jmeter](generators/jmeter.md) * [k6 (beta)](generators/k6.md) * [kotlin](generators/kotlin.md) -* [lua](generators/lua.md) +* [lua (beta)](generators/lua.md) * [nim (beta)](generators/nim.md) * [objc](generators/objc.md) * [ocaml](generators/ocaml.md) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/LuaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/LuaClientCodegen.java index 29e64126947..18330873bcf 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/LuaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/LuaClientCodegen.java @@ -21,6 +21,8 @@ import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.Schema; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; +import org.openapitools.codegen.meta.GeneratorMetadata; +import org.openapitools.codegen.meta.Stability; import org.openapitools.codegen.meta.features.*; import org.openapitools.codegen.utils.ModelUtils; import org.slf4j.Logger; @@ -79,6 +81,10 @@ public class LuaClientCodegen extends DefaultCodegen implements CodegenConfig { ) ); + generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata) + .stability(Stability.BETA) + .build(); + outputFolder = "generated-code/lua"; modelTemplateFiles.put("model.mustache", ".lua"); apiTemplateFiles.put("api.mustache", ".lua"); diff --git a/samples/client/petstore/lua/.openapi-generator/VERSION b/samples/client/petstore/lua/.openapi-generator/VERSION index 2c6109e5bb8..d99e7162d01 100644 --- a/samples/client/petstore/lua/.openapi-generator/VERSION +++ b/samples/client/petstore/lua/.openapi-generator/VERSION @@ -1 +1 @@ -3.3.4 \ No newline at end of file +5.0.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/lua/git_push.sh b/samples/client/petstore/lua/git_push.sh index 20057f67ade..ced3be2b0c7 100644 --- a/samples/client/petstore/lua/git_push.sh +++ b/samples/client/petstore/lua/git_push.sh @@ -1,11 +1,17 @@ #!/bin/sh # ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ # -# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" +# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" "gitlab.com" git_user_id=$1 git_repo_id=$2 release_note=$3 +git_host=$4 + +if [ "$git_host" = "" ]; then + git_host="github.com" + echo "[INFO] No command line input provided. Set \$git_host to $git_host" +fi if [ "$git_user_id" = "" ]; then git_user_id="GIT_USER_ID" @@ -37,9 +43,9 @@ if [ "$git_remote" = "" ]; then # git remote not defined if [ "$GIT_TOKEN" = "" ]; then 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 + git remote add origin https://${git_host}/${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 + git remote add origin https://${git_user_id}:${GIT_TOKEN}@${git_host}/${git_user_id}/${git_repo_id}.git fi fi @@ -47,6 +53,6 @@ fi git pull origin master # Pushes (Forces) the changes in the local repository up to the remote repository -echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git" +echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git" git push origin master 2>&1 | grep -v 'To https' diff --git a/samples/client/petstore/lua/petstore-1.0.0-1.rockspec b/samples/client/petstore/lua/petstore-1.0.0-1.rockspec index d6b7f2f4ccf..7f8afbe3b89 100644 --- a/samples/client/petstore/lua/petstore-1.0.0-1.rockspec +++ b/samples/client/petstore/lua/petstore-1.0.0-1.rockspec @@ -28,6 +28,8 @@ build = { ["petstore.api.user_api"] = "petstore/api/user_api.lua"; ["petstore.model.api_response"] = "petstore/model/api_response.lua"; ["petstore.model.category"] = "petstore/model/category.lua"; + ["petstore.model.inline_object"] = "petstore/model/inline_object.lua"; + ["petstore.model.inline_object_1"] = "petstore/model/inline_object_1.lua"; ["petstore.model.order"] = "petstore/model/order.lua"; ["petstore.model.pet"] = "petstore/model/pet.lua"; ["petstore.model.tag"] = "petstore/model/tag.lua"; diff --git a/samples/client/petstore/lua/petstore/api/pet_api.lua b/samples/client/petstore/lua/petstore/api/pet_api.lua index d9ce3e8df7f..02e0392c3e2 100644 --- a/samples/client/petstore/lua/petstore/api/pet_api.lua +++ b/samples/client/petstore/lua/petstore/api/pet_api.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] @@ -60,6 +60,10 @@ function pet_api:add_pet(pet) --local var_content_type = { "application/json", "application/xml" } req.headers:upsert("accept", "application/json") + -- TODO: create a function to select proper content-type + --local var_accept = { "application/xml", "application/json" } + req.headers:upsert("content-type", "application/xml") + req:set_body(dkjson.encode(pet)) -- oAuth @@ -74,7 +78,18 @@ function pet_api:add_pet(pet) end local http_status = headers:get(":status") if http_status:sub(1,1) == "2" then - return nil, headers + local body, err, errno2 = stream:get_body_as_string() + -- exception when getting the HTTP body + if not body then + return nil, err, errno2 + end + stream:shutdown() + local result, _, err3 = dkjson.decode(body) + -- exception when decoding the HTTP body + if result == nil then + return nil, err3 + end + return petstore_pet.cast(result), headers else local body, err, errno2 = stream:get_body_as_string() if not body then @@ -295,6 +310,10 @@ function pet_api:update_pet(pet) --local var_content_type = { "application/json", "application/xml" } req.headers:upsert("accept", "application/json") + -- TODO: create a function to select proper content-type + --local var_accept = { "application/xml", "application/json" } + req.headers:upsert("content-type", "application/xml") + req:set_body(dkjson.encode(pet)) -- oAuth @@ -309,7 +328,18 @@ function pet_api:update_pet(pet) end local http_status = headers:get(":status") if http_status:sub(1,1) == "2" then - return nil, headers + local body, err, errno2 = stream:get_body_as_string() + -- exception when getting the HTTP body + if not body then + return nil, err, errno2 + end + stream:shutdown() + local result, _, err3 = dkjson.decode(body) + -- exception when decoding the HTTP body + if result == nil then + return nil, err3 + end + return petstore_pet.cast(result), headers else local body, err, errno2 = stream:get_body_as_string() if not body then diff --git a/samples/client/petstore/lua/petstore/api/store_api.lua b/samples/client/petstore/lua/petstore/api/store_api.lua index 456a1bb6018..d50456ffffd 100644 --- a/samples/client/petstore/lua/petstore/api/store_api.lua +++ b/samples/client/petstore/lua/petstore/api/store_api.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] @@ -182,6 +182,10 @@ function store_api:place_order(order) -- set HTTP verb req.headers:upsert(":method", "POST") + -- TODO: create a function to select proper accept + --local var_content_type = { "application/json" } + req.headers:upsert("accept", "application/json") + -- TODO: create a function to select proper content-type --local var_accept = { "application/xml", "application/json" } req.headers:upsert("content-type", "application/xml") diff --git a/samples/client/petstore/lua/petstore/api/user_api.lua b/samples/client/petstore/lua/petstore/api/user_api.lua index 6cc3f251976..16a037608ab 100644 --- a/samples/client/petstore/lua/petstore/api/user_api.lua +++ b/samples/client/petstore/lua/petstore/api/user_api.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] @@ -55,6 +55,10 @@ function user_api:create_user(user) -- set HTTP verb req.headers:upsert(":method", "POST") + -- TODO: create a function to select proper accept + --local var_content_type = { "application/json" } + req.headers:upsert("accept", "application/json") + req:set_body(dkjson.encode(user)) @@ -88,6 +92,10 @@ function user_api:create_users_with_array_input(user) -- set HTTP verb req.headers:upsert(":method", "POST") + -- TODO: create a function to select proper accept + --local var_content_type = { "application/json" } + req.headers:upsert("accept", "application/json") + req:set_body(dkjson.encode(user)) @@ -121,6 +129,10 @@ function user_api:create_users_with_list_input(user) -- set HTTP verb req.headers:upsert(":method", "POST") + -- TODO: create a function to select proper accept + --local var_content_type = { "application/json" } + req.headers:upsert("accept", "application/json") + req:set_body(dkjson.encode(user)) @@ -308,6 +320,10 @@ function user_api:update_user(username, user) -- set HTTP verb req.headers:upsert(":method", "PUT") + -- TODO: create a function to select proper accept + --local var_content_type = { "application/json" } + req.headers:upsert("accept", "application/json") + req:set_body(dkjson.encode(user)) diff --git a/samples/client/petstore/lua/petstore/model/api_response.lua b/samples/client/petstore/lua/petstore/model/api_response.lua index e96796dc5d0..9531219b71b 100644 --- a/samples/client/petstore/lua/petstore/model/api_response.lua +++ b/samples/client/petstore/lua/petstore/model/api_response.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] diff --git a/samples/client/petstore/lua/petstore/model/category.lua b/samples/client/petstore/lua/petstore/model/category.lua index 711124a4fbe..fd15b89da0e 100644 --- a/samples/client/petstore/lua/petstore/model/category.lua +++ b/samples/client/petstore/lua/petstore/model/category.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] diff --git a/samples/client/petstore/lua/petstore/model/inline_object.lua b/samples/client/petstore/lua/petstore/model/inline_object.lua new file mode 100644 index 00000000000..c09af285795 --- /dev/null +++ b/samples/client/petstore/lua/petstore/model/inline_object.lua @@ -0,0 +1,32 @@ +--[[ + OpenAPI Petstore + + This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + + The version of the OpenAPI document: 1.0.0 + + Generated by: https://openapi-generator.tech +]] + +-- inline_object class +local inline_object = {} +local inline_object_mt = { + __name = "inline_object"; + __index = inline_object; +} + +local function cast_inline_object(t) + return setmetatable(t, inline_object_mt) +end + +local function new_inline_object(name, status) + return cast_inline_object({ + ["name"] = name; + ["status"] = status; + }) +end + +return { + cast = cast_inline_object; + new = new_inline_object; +} diff --git a/samples/client/petstore/lua/petstore/model/inline_object_1.lua b/samples/client/petstore/lua/petstore/model/inline_object_1.lua new file mode 100644 index 00000000000..a7d19b29997 --- /dev/null +++ b/samples/client/petstore/lua/petstore/model/inline_object_1.lua @@ -0,0 +1,32 @@ +--[[ + OpenAPI Petstore + + This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + + The version of the OpenAPI document: 1.0.0 + + Generated by: https://openapi-generator.tech +]] + +-- inline_object_1 class +local inline_object_1 = {} +local inline_object_1_mt = { + __name = "inline_object_1"; + __index = inline_object_1; +} + +local function cast_inline_object_1(t) + return setmetatable(t, inline_object_1_mt) +end + +local function new_inline_object_1(additional_metadata, file) + return cast_inline_object_1({ + ["additionalMetadata"] = additional_metadata; + ["file"] = file; + }) +end + +return { + cast = cast_inline_object_1; + new = new_inline_object_1; +} diff --git a/samples/client/petstore/lua/petstore/model/order.lua b/samples/client/petstore/lua/petstore/model/order.lua index 3b67e997f9e..48a691b72d7 100644 --- a/samples/client/petstore/lua/petstore/model/order.lua +++ b/samples/client/petstore/lua/petstore/model/order.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] diff --git a/samples/client/petstore/lua/petstore/model/pet.lua b/samples/client/petstore/lua/petstore/model/pet.lua index 3a750e3a1a3..5f887e32b55 100644 --- a/samples/client/petstore/lua/petstore/model/pet.lua +++ b/samples/client/petstore/lua/petstore/model/pet.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] diff --git a/samples/client/petstore/lua/petstore/model/tag.lua b/samples/client/petstore/lua/petstore/model/tag.lua index 7daf7fcc722..872dfab64bc 100644 --- a/samples/client/petstore/lua/petstore/model/tag.lua +++ b/samples/client/petstore/lua/petstore/model/tag.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] diff --git a/samples/client/petstore/lua/petstore/model/user.lua b/samples/client/petstore/lua/petstore/model/user.lua index c7cb44013b6..82539d731da 100644 --- a/samples/client/petstore/lua/petstore/model/user.lua +++ b/samples/client/petstore/lua/petstore/model/user.lua @@ -3,7 +3,7 @@ This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - OpenAPI spec version: 1.0.0 + The version of the OpenAPI document: 1.0.0 Generated by: https://openapi-generator.tech ]] diff --git a/samples/client/petstore/lua/spec/inline_object_1_spec.lua b/samples/client/petstore/lua/spec/inline_object_1_spec.lua new file mode 100644 index 00000000000..ba0da43dd7a --- /dev/null +++ b/samples/client/petstore/lua/spec/inline_object_1_spec.lua @@ -0,0 +1,33 @@ +--[[ + OpenAPI Petstore + + This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + + The version of the OpenAPI document: 1.0.0 + + Generated by: https://openapi-generator.tech +]] + +--[[ +Unit tests for petstore.model.inline_object_1 +Automatically generated by openapi-generator (https://openapi-generator.tech) +Please update as you see appropriate +]] +describe("inline_object_1", function() + local petstore_inline_object_1 = require "petstore.model.inline_object_1" + + -- unit tests for the property 'additional_metadata' + describe("property additional_metadata test", function() + it("should work", function() + -- TODO assertion here: http://olivinelabs.com/busted/#asserts + end) + end) + + -- unit tests for the property 'file' + describe("property file test", function() + it("should work", function() + -- TODO assertion here: http://olivinelabs.com/busted/#asserts + end) + end) + +end) diff --git a/samples/client/petstore/lua/spec/inline_object_spec.lua b/samples/client/petstore/lua/spec/inline_object_spec.lua new file mode 100644 index 00000000000..00d34720f83 --- /dev/null +++ b/samples/client/petstore/lua/spec/inline_object_spec.lua @@ -0,0 +1,33 @@ +--[[ + OpenAPI Petstore + + This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + + The version of the OpenAPI document: 1.0.0 + + Generated by: https://openapi-generator.tech +]] + +--[[ +Unit tests for petstore.model.inline_object +Automatically generated by openapi-generator (https://openapi-generator.tech) +Please update as you see appropriate +]] +describe("inline_object", function() + local petstore_inline_object = require "petstore.model.inline_object" + + -- unit tests for the property 'name' + describe("property name test", function() + it("should work", function() + -- TODO assertion here: http://olivinelabs.com/busted/#asserts + end) + end) + + -- unit tests for the property 'status' + describe("property status test", function() + it("should work", function() + -- TODO assertion here: http://olivinelabs.com/busted/#asserts + end) + end) + +end)