Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions compiler/cpp/src/thrift/generate/t_netstd_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ void t_netstd_generator::generate_netstd_struct_definition(ostream& out, t_struc
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter)
{
generate_netstd_doc(out, *m_iter);
generate_property(out, *m_iter, true, true);
generate_property(out, *m_iter, true, true, is_exception);
bool is_required = field_is_required((*m_iter));
if (is_required)
{
Expand Down Expand Up @@ -3379,12 +3379,12 @@ void t_netstd_generator::generate_serialize_list_element(ostream& out, t_list* t
generate_serialize_field(out, &efield, "", false, false);
}

void t_netstd_generator::generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset)
void t_netstd_generator::generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, bool in_exception)
{
generate_netstd_property(out, tfield, isPublic, generateIsset, "_");
generate_netstd_property(out, tfield, isPublic, generateIsset, "_", in_exception);
}

void t_netstd_generator::generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, string fieldPrefix)
void t_netstd_generator::generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, string fieldPrefix, bool in_exception)
{
if ((is_serialize_enabled() || is_wcf_enabled()) && isPublic)
{
Expand All @@ -3394,6 +3394,7 @@ void t_netstd_generator::generate_netstd_property(ostream& out, t_field* tfield,

out << indent()
<< (isPublic ? "public " : "private ")
<< (hides_inherited_member(prop_name(tfield), in_exception) ? "new " : "")
<< type_name(tfield->get_type())
<< nullable_field_suffix(tfield)
<< " "
Expand Down Expand Up @@ -3654,6 +3655,26 @@ string t_netstd_generator::get_isset_name(const string& str) {
}


// Whether a member of that name hides one that every generated class inherits from System.Object,
// or that an exception inherits from System.Exception. Such a member is declared "new", which keeps
// the inherited one intact (CS0108, CS0114). Equals(), GetHashCode() and ToString() are left out,
// as the generated classes declare them themselves, and so is Finalize(), which cannot be hidden.
bool t_netstd_generator::hides_inherited_member(const string& name, bool in_exception) {
static const std::set<string> object_members = {
"GetType", "MemberwiseClone", "ReferenceEquals"
};
static const std::set<string> exception_members = {
"Data", "GetBaseException", "GetObjectData", "HelpLink", "HResult", "InnerException",
"Message", "SerializeObjectState", "Source", "StackTrace", "TargetSite"
};

if (object_members.find(name) != object_members.end()) {
return true;
}
return in_exception && (exception_members.find(name) != exception_members.end());
}


string t_netstd_generator::prop_name(t_field* tfield, bool suppress_mapping) {
string name(tfield->get_name());
if (suppress_mapping) {
Expand Down
5 changes: 3 additions & 2 deletions compiler/cpp/src/thrift/generate/t_netstd_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class t_netstd_generator : public t_oop_generator
// additional files
void generate_extensions_file();

void generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset);
void generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool includeIsset = true, string fieldPrefix = "");
void generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, bool in_exception = false);
void generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool includeIsset = true, string fieldPrefix = "", bool in_exception = false);
bool print_const_value(ostream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval = false, bool needtype = false);
string render_const_value(ostream& out, string name, t_type* type, t_const_value* value);
void print_const_constructor(ostream& out, vector<t_const*> consts);
Expand Down Expand Up @@ -210,6 +210,7 @@ class t_netstd_generator : public t_oop_generator
void cleanup_member_name_mapping(void* scope);
string get_mapped_member_name(string oldname);
string get_isset_name(const string& str);
bool hides_inherited_member(const string& name, bool in_exception);
string get_deep_copy_method_call(t_type* ttype, bool is_not_null, bool& needs_typecast, string& suffix);
string deep_copy_expression(const string& source, t_type* ttype, bool is_not_null, bool& needs_typecast, bool null_conditional = false);
string extensions_class_name(t_program* program);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<Nullable>enable</Nullable>
<!-- generated members that hide inherited ones must say so, and only those -->
<WarningsAsErrors>$(WarningsAsErrors);CS0108;CS0109;CS0114</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -80,6 +82,7 @@
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net10 -r ../../../../../test/ExceptionStruct.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net10 -r ../../../../../test/Recursive.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net10 -r ../../../../../contrib/fb303/if/fb303.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net10 -r ../Thrift4833.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net10 -r ../Thrift5253.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net10 -r ../Thrift5320.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net10 -r ../Thrift5382.thrift" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<Nullable>enable</Nullable>
<!-- generated members that hide inherited ones must say so, and only those -->
<WarningsAsErrors>$(WarningsAsErrors);CS0108;CS0109;CS0114</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -81,6 +83,7 @@
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net8 -r ../../../../../test/ThriftTest.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net8 -r ../../../../../test/ExceptionStruct.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net8 -r ../../../../../contrib/fb303/if/fb303.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net8 -r ../Thrift4833.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net8 -r ../Thrift5253.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net8 -r ../Thrift5320.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net8 -r ../Thrift5382.thrift" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<Nullable>enable</Nullable>
<!-- generated members that hide inherited ones must say so, and only those -->
<WarningsAsErrors>$(WarningsAsErrors);CS0108;CS0109;CS0114</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -81,6 +83,7 @@
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net9 -r ../../../../../test/ThriftTest.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net9 -r ../../../../../test/ExceptionStruct.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net9 -r ../../../../../contrib/fb303/if/fb303.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net9 -r ../Thrift4833.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net9 -r ../Thrift5253.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net9 -r ../Thrift5320.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:wcf,union,serial,net9 -r ../Thrift5382.thrift" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<!-- generated members that hide inherited ones must say so, and only those -->
<WarningsAsErrors>$(WarningsAsErrors);CS0108;CS0109;CS0114</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -72,6 +74,7 @@
<Exec Command="$(PathToThrift) -gen netstd:union,serial -r ../name_conflicts.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:union,serial -r ../../../../../test/ThriftTest.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:union,serial -r ../../../../../contrib/fb303/if/fb303.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:union,serial -r ../Thrift4833.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:union,serial -r ../Thrift5253.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:union,serial -r ../Thrift5320.thrift" />
<Exec Command="$(PathToThrift) -gen netstd:union,serial -r ../Thrift5382.thrift" />
Expand Down
75 changes: 75 additions & 0 deletions lib/netstd/Tests/Thrift.Compile.Tests/Thrift4833.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.
*/

// Testcase for THRIFT-4833 warning CS0114 "Xception.Message" hides inherited member "Exception.Message"
//
// A field becomes a property of the same name, starting with an uppercase letter. Every generated
// class inherits GetType(), MemberwiseClone() and ReferenceEquals() from System.Object, and an
// exception inherits the members of System.Exception as well. Finalize() cannot be hidden.

namespace * Thrift4833

exception InheritedNames {
1: string message
2: string data
3: string source
4: string helpLink
5: string stackTrace
6: string innerException
7: i32 hResult
8: string targetSite
9: string getBaseException
10: string getObjectData
11: string getType
12: string serializeObjectState
13: string memberwiseClone
14: string referenceEquals
15: string finalize
}

exception RequiredInheritedNames {
1: required string message
2: required string data
3: required string source
4: required string helpLink
5: required string stackTrace
6: required string innerException
7: required i32 hResult
8: required string targetSite
9: required string getBaseException
10: required string getObjectData
11: required string getType
12: required string serializeObjectState
13: required string memberwiseClone
14: required string referenceEquals
15: required string finalize
}

struct ObjectNames {
1: string getType
2: string memberwiseClone
3: string referenceEquals
4: string finalize
5: string message
6: string data
}

service InheritedNamesService {
string Call(1: string getType, 2: string memberwiseClone, 3: string referenceEquals, 4: string message) throws (1: InheritedNames getType)
}
129 changes: 129 additions & 0 deletions lib/netstd/Tests/Thrift.Tests/DataModel/InheritedMemberNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// 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.

using System;
using System.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Thrift4833;
using ThriftTest;
using ErrorData = Thrift5320.exceptions.ErrorData;
using TaskException = Thrift5320.exceptions.Task;

namespace Thrift.Tests.DataModel
{
// THRIFT-4833: a field whose name is that of an inherited member hides that member. The generated
// class returns the field, while the inherited member stays what it is, seen through the base class.
[TestClass]
public class InheritedMemberNamesTests
{
[TestMethod]
public void FieldNamedMessage_HidesExceptionMessage()
{
var xception = new Xception { ErrorCode = 1001, Message = "This is an Xception" };
Assert.AreEqual("This is an Xception", xception.Message);

Exception exception = xception;
Assert.AreEqual(((Exception)new Xception()).Message, exception.Message);
Assert.AreNotEqual(xception.Message, exception.Message);
}

[TestMethod]
public void FieldNamedData_HidesExceptionData()
{
var data = new ErrorData { Messitsch = "details" };
var task = new TaskException { Data = data };
Assert.AreSame(data, task.Data);

Exception exception = task;
Assert.IsInstanceOfType<IDictionary>(exception.Data);
}

[TestMethod]
public void FieldsNamedAfterInheritedMembers_HideThem()
{
var names = new InheritedNames
{
Message = "message",
Data = "data",
Source = "source",
HelpLink = "helpLink",
StackTrace = "stackTrace",
InnerException = "innerException",
HResult = 42,
TargetSite = "targetSite",
GetBaseException = "getBaseException",
GetObjectData = "getObjectData",
GetType = "getType",
SerializeObjectState = "serializeObjectState",
MemberwiseClone = "memberwiseClone",
ReferenceEquals = "referenceEquals",
Finalize = "finalize",
};

Exception thrown;
try
{
throw names;
}
catch (InheritedNames caught)
{
thrown = caught;
}
Assert.AreSame(names, thrown);

// the fields
Assert.AreEqual("message", names.Message);
Assert.AreEqual("data", names.Data);
Assert.AreEqual("source", names.Source);
Assert.AreEqual("helpLink", names.HelpLink);
Assert.AreEqual("stackTrace", names.StackTrace);
Assert.AreEqual("innerException", names.InnerException);
Assert.AreEqual(42, names.HResult);
Assert.AreEqual("targetSite", names.TargetSite);
Assert.AreEqual("getBaseException", names.GetBaseException);
Assert.AreEqual("getObjectData", names.GetObjectData);
Assert.AreEqual("getType", names.GetType);
Assert.AreEqual("serializeObjectState", names.SerializeObjectState);
Assert.AreEqual("memberwiseClone", names.MemberwiseClone);
Assert.AreEqual("referenceEquals", names.ReferenceEquals);
Assert.AreEqual("finalize", names.Finalize);

// the inherited members
Assert.AreEqual(((Exception)new InheritedNames()).Message, thrown.Message);
Assert.IsInstanceOfType<IDictionary>(thrown.Data);
Assert.AreEqual(typeof(InheritedMemberNamesTests).Assembly.GetName().Name, thrown.Source);
Assert.IsNull(thrown.HelpLink);
Assert.IsNotNull(thrown.StackTrace);
Assert.Contains(nameof(FieldsNamedAfterInheritedMembers_HideThem), thrown.StackTrace);
Assert.IsNull(thrown.InnerException);
Assert.AreEqual(new Exception().HResult, thrown.HResult);
Assert.AreEqual(nameof(FieldsNamedAfterInheritedMembers_HideThem), thrown.TargetSite!.Name);
Assert.AreSame(thrown, thrown.GetBaseException());
Assert.AreEqual(typeof(InheritedNames), thrown.GetType());
}

[TestMethod]
public void StructFieldsNamedAfterObjectMembers_HideThem()
{
var names = new ObjectNames { GetType = "getType", MemberwiseClone = "memberwiseClone", ReferenceEquals = "referenceEquals" };
Assert.AreEqual("getType", names.GetType);
Assert.AreEqual("memberwiseClone", names.MemberwiseClone);
Assert.AreEqual("referenceEquals", names.ReferenceEquals);
Assert.AreEqual(typeof(ObjectNames), ((object)names).GetType());
}
}
}
Loading