summaryrefslogtreecommitdiff
path: root/compiler
diff options
context:
space:
mode:
authorPhil Price <philprice@gmail.com>2021-10-09 17:35:23 -0700
committerJens Geyer <Jens-G@users.noreply.github.com>2022-04-21 09:07:23 +0200
commit625367f2169848802c9b885249571d8e4b3fcc6e (patch)
treec49a7ec0f329616c5b87a466ce7d8cc1040187cf /compiler
parent49b2d6b888a2a96fc0948da81a779a90b4624170 (diff)
downloadthrift-625367f2169848802c9b885249571d8e4b3fcc6e.tar.gz
(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
Diffstat (limited to 'compiler')
-rw-r--r--compiler/cpp/src/thrift/generate/t_js_generator.cc19
1 files 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 += ", ";