Skip to content

Commit

Permalink
Fix bug where enum properties were mistakenly parsed as uint when the…
Browse files Browse the repository at this point in the history
…y were string
  • Loading branch information
dcronqvist committed Nov 27, 2024
1 parent f3c4478 commit ade3d88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
21 changes: 9 additions & 12 deletions src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ internal List<IProperty> ReadPropertiesInsideClass(
return resultingProps;
}

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

if (!customTypeDef.HasValue)
{
if (typeInJson == PropertyType.String)
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
#pragma warning disable IDE0072 // Add missing cases
return typeInJson switch
{
var value = element.GetRequiredProperty<string>("value");
var values = value.Split(',').Select(v => v.Trim()).ToHashSet();
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
}
else
{
var value = element.GetRequiredProperty<int>("value");
var values = new HashSet<string> { value.ToString(CultureInfo.InvariantCulture) };
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
}
PropertyType.String => new StringProperty { Name = name, Value = element.GetRequiredProperty<string>("value") },
PropertyType.Int => new IntProperty { Name = name, Value = element.GetRequiredProperty<int>("value") },
};
#pragma warning restore IDE0072 // Add missing cases
#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
}

if (customTypeDef.Value is not CustomEnumDefinition ced)
Expand Down
23 changes: 10 additions & 13 deletions src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ internal ClassProperty ReadClassProperty()
return new ClassProperty { Name = name, PropertyType = propertyType, Value = propsInType };
}

internal EnumProperty ReadEnumProperty()
internal IProperty ReadEnumProperty()
{
var name = _reader.GetRequiredAttribute("name");
var propertyType = _reader.GetRequiredAttribute("propertytype");
Expand All @@ -132,25 +132,22 @@ internal EnumProperty ReadEnumProperty()
"string" => PropertyType.String,
"int" => PropertyType.Int,
_ => throw new XmlException("Invalid property type")
}) ?? PropertyType.String;
}).GetValueOr(PropertyType.String);
var customTypeDef = _customTypeResolver(propertyType);

// If the custom enum definition is not found,
// we assume an empty enum definition.
if (!customTypeDef.HasValue)
{
if (typeInXml == PropertyType.String)
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
#pragma warning disable IDE0072 // Add missing cases
return typeInXml switch
{
var value = _reader.GetRequiredAttribute("value");
var values = value.Split(',').Select(v => v.Trim()).ToHashSet();
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
}
else
{
var value = _reader.GetRequiredAttributeParseable<int>("value");
var values = new HashSet<string> { value.ToString(CultureInfo.InvariantCulture) };
return new EnumProperty { Name = name, PropertyType = propertyType, Value = values };
}
PropertyType.String => new StringProperty { Name = name, Value = _reader.GetRequiredAttribute("value") },
PropertyType.Int => new IntProperty { Name = name, Value = _reader.GetRequiredAttributeParseable<int>("value") },
};
#pragma warning restore IDE0072 // Add missing cases
#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
}

if (customTypeDef.Value is not CustomEnumDefinition ced)
Expand Down

0 comments on commit ade3d88

Please sign in to comment.