Skip to content

Commit ade3d88

Browse files
committed
Fix bug where enum properties were mistakenly parsed as uint when they were string
1 parent f3c4478 commit ade3d88

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ internal List<IProperty> ReadPropertiesInsideClass(
156156
return resultingProps;
157157
}
158158

159-
internal EnumProperty ReadEnumProperty(JsonElement element)
159+
internal IProperty ReadEnumProperty(JsonElement element)
160160
{
161161
var name = element.GetRequiredProperty<string>("name");
162162
var propertyType = element.GetRequiredProperty<string>("propertytype");
@@ -170,18 +170,15 @@ internal EnumProperty ReadEnumProperty(JsonElement element)
170170

171171
if (!customTypeDef.HasValue)
172172
{
173-
if (typeInJson == PropertyType.String)
173+
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
174+
#pragma warning disable IDE0072 // Add missing cases
175+
return typeInJson switch
174176
{
175-
var value = element.GetRequiredProperty<string>("value");
176-
var values = value.Split(',').Select(v => v.Trim()).ToHashSet();
177-
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
178-
}
179-
else
180-
{
181-
var value = element.GetRequiredProperty<int>("value");
182-
var values = new HashSet<string> { value.ToString(CultureInfo.InvariantCulture) };
183-
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
184-
}
177+
PropertyType.String => new StringProperty { Name = name, Value = element.GetRequiredProperty<string>("value") },
178+
PropertyType.Int => new IntProperty { Name = name, Value = element.GetRequiredProperty<int>("value") },
179+
};
180+
#pragma warning restore IDE0072 // Add missing cases
181+
#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
185182
}
186183

187184
if (customTypeDef.Value is not CustomEnumDefinition ced)

src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ internal ClassProperty ReadClassProperty()
123123
return new ClassProperty { Name = name, PropertyType = propertyType, Value = propsInType };
124124
}
125125

126-
internal EnumProperty ReadEnumProperty()
126+
internal IProperty ReadEnumProperty()
127127
{
128128
var name = _reader.GetRequiredAttribute("name");
129129
var propertyType = _reader.GetRequiredAttribute("propertytype");
@@ -132,25 +132,22 @@ internal EnumProperty ReadEnumProperty()
132132
"string" => PropertyType.String,
133133
"int" => PropertyType.Int,
134134
_ => throw new XmlException("Invalid property type")
135-
}) ?? PropertyType.String;
135+
}).GetValueOr(PropertyType.String);
136136
var customTypeDef = _customTypeResolver(propertyType);
137137

138138
// If the custom enum definition is not found,
139139
// we assume an empty enum definition.
140140
if (!customTypeDef.HasValue)
141141
{
142-
if (typeInXml == PropertyType.String)
142+
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
143+
#pragma warning disable IDE0072 // Add missing cases
144+
return typeInXml switch
143145
{
144-
var value = _reader.GetRequiredAttribute("value");
145-
var values = value.Split(',').Select(v => v.Trim()).ToHashSet();
146-
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
147-
}
148-
else
149-
{
150-
var value = _reader.GetRequiredAttributeParseable<int>("value");
151-
var values = new HashSet<string> { value.ToString(CultureInfo.InvariantCulture) };
152-
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
153-
}
146+
PropertyType.String => new StringProperty { Name = name, Value = _reader.GetRequiredAttribute("value") },
147+
PropertyType.Int => new IntProperty { Name = name, Value = _reader.GetRequiredAttributeParseable<int>("value") },
148+
};
149+
#pragma warning restore IDE0072 // Add missing cases
150+
#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
154151
}
155152

156153
if (customTypeDef.Value is not CustomEnumDefinition ced)

0 commit comments

Comments
 (0)