You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2022/04/21 07:07:30 UTC
[thrift] branch master updated: (typescript): Fix invalid optional members and argument generation
This is an automated email from the ASF dual-hosted git repository.
jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git
The following commit(s) were added to refs/heads/master by this push:
new 625367f21 (typescript): Fix invalid optional members and argument generation
625367f21 is described below
commit 625367f2169848802c9b885249571d8e4b3fcc6e
Author: Phil Price <ph...@gmail.com>
AuthorDate: Sat Oct 9 17:35:23 2021 -0700
(typescript): Fix invalid optional members and argument generation
Fixes two cases where the optional flag `?` is generated incorrectly for typescript, leading to invalid build:
- Non-optional function arguments after optional arguments
- Exception types with optional message
---
compiler/cpp/src/thrift/generate/t_js_generator.cc | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/compiler/cpp/src/thrift/generate/t_js_generator.cc b/compiler/cpp/src/thrift/generate/t_js_generator.cc
index 426b0e29d..5fcac1659 100644
--- a/compiler/cpp/src/thrift/generate/t_js_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_js_generator.cc
@@ -910,7 +910,14 @@ void t_js_generator::generate_js_struct_definition(ostream& out,
}
if (gen_ts_) {
string ts_access = gen_node_ ? "public " : "";
- f_types_ts_ << ts_indent() << ts_access << (*m_iter)->get_name() << ts_get_req(*m_iter) << ": "
+ string member_name = (*m_iter)->get_name();
+
+ // Special case. Exceptions derive from Error, and error has a non optional message field.
+ // Ignore the optional flag in this case, otherwise we will generate a incompatible field
+ // in the eyes of typescript.
+ string optional_flag = is_exception && member_name == "message" ? "" : ts_get_req(*m_iter);
+
+ f_types_ts_ << ts_indent() << ts_access << member_name << optional_flag << ": "
<< ts_get_type((*m_iter)->get_type()) << ";" << endl;
}
}
@@ -2802,8 +2809,16 @@ std::string t_js_generator::ts_function_signature(t_function* tfunction, bool in
str = tfunction->get_name() + "(";
+ bool has_written_optional = false;
+
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- str += (*f_iter)->get_name() + ts_get_req(*f_iter) + ": " + ts_get_type((*f_iter)->get_type());
+ // Ensure that non optional parameters do not follow optional parameters
+ // E.g. public foo(a: string, b?: string; c: string) is invalid, c must be optional, or b non-optional
+ string original_optional = ts_get_req(*f_iter);
+ string optional = has_written_optional ? "?" : original_optional;
+ has_written_optional = has_written_optional || optional.size() > 0;
+
+ str += (*f_iter)->get_name() + optional + ": " + ts_get_type((*f_iter)->get_type());
if (f_iter + 1 != fields.end() || (include_callback && fields.size() > 0)) {
str += ", ";