diff --git a/compiler/cpp/src/thrift/generate/t_haxe_generator.cc b/compiler/cpp/src/thrift/generate/t_haxe_generator.cc index bfd01060605..9b2f7860809 100644 --- a/compiler/cpp/src/thrift/generate/t_haxe_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_haxe_generator.cc @@ -147,7 +147,9 @@ class t_haxe_generator : public t_oop_generator { void generate_generic_isset_method(std::ostream& out, t_struct* tstruct); void generate_property_getters_setters(std::ostream& out, t_struct* tstruct); - void generate_function_helpers(t_function* tfunction); + void generate_function_helpers(t_service* tservice, t_function* tfunction); + std::string args_struct_name(t_service* tservice, t_function* tfunction); + std::string result_struct_name(t_service* tservice, t_function* tfunction); std::string get_cap_name(std::string name); std::string make_haxe_user_type_name(const std::string& name); std::string escape_haxe_keyword(std::string name); @@ -1820,11 +1822,30 @@ void t_haxe_generator::generate_service_helpers(t_service* tservice) { vector::iterator f_iter; for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { t_struct* ts = (*f_iter)->get_arglist(); + string name_orig = ts->get_name(); + ts->set_name(args_struct_name(tservice, *f_iter)); generate_haxe_struct(ts, false); - generate_function_helpers(*f_iter); + ts->set_name(name_orig); + generate_function_helpers(tservice, *f_iter); } } +/** + * The names of the argument and result structs of a method. These are classes of the package, as + * are those of the other services in it, so they carry the name of the service that declares the + * method. + * + * @param tservice The service that declares the method + * @param tfunction The method + */ +string t_haxe_generator::args_struct_name(t_service* tservice, t_function* tfunction) { + return get_cap_name(tservice->get_name() + "_" + tfunction->get_name() + "_args"); +} + +string t_haxe_generator::result_struct_name(t_service* tservice, t_function* tfunction) { + return get_cap_name(tservice->get_name() + "_" + tfunction->get_name() + "_result"); +} + /** * Generates a service client definition. * @@ -1895,7 +1916,7 @@ void t_haxe_generator::generate_service_client(t_service* tservice) { // Get the struct of function call params t_struct* arg_struct = (*f_iter)->get_arglist(); - string argsname = get_cap_name((*f_iter)->get_name() + "_args"); + string argsname = args_struct_name(tservice, *f_iter); vector::const_iterator fld_iter; const vector& fields = arg_struct->get_members(); @@ -1930,7 +1951,7 @@ void t_haxe_generator::generate_service_client(t_service* tservice) { indent_up(); string appex = tmp("appex"); indent(f_service_) << "var " << appex << " : TApplicationException;" << '\n'; - string resultname = get_cap_name((*f_iter)->get_name() + "_result"); + string resultname = result_struct_name(tservice, *f_iter); indent(f_service_) << "if (error != null) {" << '\n'; indent_up(); indent(f_service_) << "if (onError == null)" << '\n'; @@ -2137,14 +2158,15 @@ void t_haxe_generator::generate_service_server(t_service* tservice) { /** * Generates a struct and helpers for a function. * + * @param tservice The service that declares the function * @param tfunction The function */ -void t_haxe_generator::generate_function_helpers(t_function* tfunction) { +void t_haxe_generator::generate_function_helpers(t_service* tservice, t_function* tfunction) { if (tfunction->is_oneway()) { return; } - string resultname = get_cap_name(tfunction->get_name() + "_result"); + string resultname = result_struct_name(tservice, tfunction); t_struct result(program_, resultname); t_field success(tfunction->get_returntype(), "success", 0); if (!tfunction->get_returntype()->is_void()) { @@ -2167,7 +2189,6 @@ void t_haxe_generator::generate_function_helpers(t_function* tfunction) { * @param tfunction The function to write a dispatcher for */ void t_haxe_generator::generate_process_function(t_service* tservice, t_function* tfunction) { - (void)tservice; // Open class indent(f_service_) << "private function " << tfunction->get_name() << "() : Int->TProtocol->TProtocol->Void {" << '\n'; @@ -2178,8 +2199,8 @@ void t_haxe_generator::generate_process_function(t_service* tservice, t_function << '\n'; scope_up(f_service_); - string argsname = get_cap_name(tfunction->get_name() + "_args"); - string resultname = get_cap_name(tfunction->get_name() + "_result"); + string argsname = args_struct_name(tservice, tfunction); + string resultname = result_struct_name(tservice, tfunction); f_service_ << indent() << "var args : " << argsname << " = new " << argsname << "();" << '\n' << indent() << "args.read(iprot);" << '\n' << indent() << "iprot.readMessageEnd();" diff --git a/compiler/cpp/tests/haxe/t_haxe_generator_service_tests.cc b/compiler/cpp/tests/haxe/t_haxe_generator_service_tests.cc new file mode 100644 index 00000000000..bcf0972170f --- /dev/null +++ b/compiler/cpp/tests/haxe/t_haxe_generator_service_tests.cc @@ -0,0 +1,100 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "../cpp/t_cpp_generator_test_utils.h" + +#include +#include +#include + +using std::map; +using std::string; +using cpp_generator_test_utils::parse_thrift_for_test; +using cpp_generator_test_utils::read_file; + +// THRIFT-6529: the argument and result structs of a method are classes of the package. They were named after the +// method alone, so two services of one package with a method of the same name wrote the same classes. +TEST_CASE("t_haxe_generator names argument and result structs after service and method", "[functional]") +{ + const string thrift_path = "test_haxe_service_helpers.thrift"; + const string thrift_source = + "service A {\n" + " i32 get(1: i32 a)\n" + "}\n" + "\n" + "service B {\n" + " string get()\n" + "}\n" + "\n" + "service C extends A {\n" + " void put(1: i32 c)\n" + "}\n"; + + // files of an earlier run must not count + const string checked[] = { + "A_get_args.hx", "A_get_result.hx", "AImpl.hx", "AProcessor.hx", + "B_get_args.hx", "B_get_result.hx", "BImpl.hx", "BProcessor.hx", + "C_get_args.hx", "C_put_args.hx", "CImpl.hx" + }; + for (const string& file : checked) { + std::remove(("gen-haxe/" + file).c_str()); + } + + { + std::ofstream thrift_file(thrift_path, std::ios::binary); + REQUIRE(thrift_file.is_open()); + thrift_file << thrift_source; + } + + map parsed_options; + std::unique_ptr program(new t_program(thrift_path, "test_haxe_service_helpers")); + parse_thrift_for_test(program.get()); + + std::unique_ptr gen( + t_generator_registry::get_generator(program.get(), "haxe", parsed_options, "")); + REQUIRE(gen != nullptr); + REQUIRE_NOTHROW(gen->generate_program()); + + // each service has structs of its own + const string a_args = read_file("gen-haxe/A_get_args.hx"); + REQUIRE(a_args.find("class A_get_args ") != string::npos); + REQUIRE(a_args.find("public var a(get,set) : haxe.Int32;") != string::npos); + const string b_args = read_file("gen-haxe/B_get_args.hx"); + REQUIRE(b_args.find("class B_get_args ") != string::npos); + REQUIRE(b_args.find("public var a(") == string::npos); + REQUIRE(read_file("gen-haxe/A_get_result.hx").find("public var success(get,set) : haxe.Int32;") != string::npos); + REQUIRE(read_file("gen-haxe/B_get_result.hx").find("public var success(get,set) : String;") != string::npos); + + // and the client and the processor of each service use them + for (const string service : { "A", "B" }) { + const string impl = read_file("gen-haxe/" + service + "Impl.hx"); + REQUIRE(impl.find(" = new " + service + "_get_args();") != string::npos); + REQUIRE(impl.find(" = new " + service + "_get_result();") != string::npos); + const string processor = read_file("gen-haxe/" + service + "Processor.hx"); + REQUIRE(processor.find("var args : " + service + "_get_args = new " + service + "_get_args();") + != string::npos); + REQUIRE(processor.find("var result : " + service + "_get_result = new " + service + "_get_result();") + != string::npos); + } + + // an inherited method keeps the structs of the service that declares it + REQUIRE(read_file("gen-haxe/C_put_args.hx").find("class C_put_args ") != string::npos); + REQUIRE(read_file("gen-haxe/C_get_args.hx").empty()); + REQUIRE(read_file("gen-haxe/CImpl.hx").find("_get_args") == string::npos); + + std::remove(thrift_path.c_str()); +}