Fix bug where enum properties were mistakenly parsed as uint when they were string

This commit is contained in:
Daniel Cronqvist 2024-11-27 22:15:24 +01:00
parent f3c4478125
commit ade3d8840a
2 changed files with 19 additions and 25 deletions

View file

@ -156,7 +156,7 @@ public abstract partial class TmjReaderBase
return resultingProps;
}
internal EnumProperty ReadEnumProperty(JsonElement element)
internal IProperty ReadEnumProperty(JsonElement element)
{
var name = element.GetRequiredProperty<string>("name");
var propertyType = element.GetRequiredProperty<string>("propertytype");
@ -170,18 +170,15 @@ public abstract partial class TmjReaderBase
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)

View file

@ -123,7 +123,7 @@ public abstract partial class TmxReaderBase
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");
@ -132,25 +132,22 @@ public abstract partial class TmxReaderBase
"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)