Skip to content

JsonStringEnumConverter ignores 'allowIntegerValues' when deserializing quoted numeric values #58247

Closed

Description

@DenisAloner

.NET 5
JsonStringEnumConverter ignores JsonNumberHandling.Strict on deserialization.
Example: https://dotnetfiddle.net/IsP6Dn

Activity

  1. ghost added
    untriaged
    on Aug 27, 2021
  2. eiriktsarpalis commented on Aug 27, 2021

    @eiriktsarpalis
    Member

    This by design, number handling only applies to numeric types and not enums (this includes the default enum converter and not just JsonStringEnumConverter).

    Out of curiosity, what would be your expected behavior in that snippet? Assuming strict number handling was respected, it would directly contradict what JsonStringEnumConverter is supposed to do (that is, express enums as strings as opposed to their underlying numeric values).

  3. DenisAloner commented on Aug 27, 2021

    @DenisAloner
    Author

    Thank you for your quick response!
    I expected that these options will result in the following:
    {"Value" : 0} - not valid
    {"Value" : "0"} - not valid
    {"Value" : "On"} - valid
    {"Value" : "Off"} - valid
    But the example shows that these options give the following:
    {"Value" : 0} - not valid
    {"Value" : "0"} - valid
    {"Value" : "On"} - valid
    {"Value" : "Off"} - valid

  4. eiriktsarpalis commented on Aug 27, 2021

    @eiriktsarpalis
    Member

    I see; while the issue is unrelated to JsonNumberHandling, the allowIntegerValues parameter is not honored when the undefined enum is encoded as a string. Minimal reproduction:

    using System;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    public class Program
    {
        public static void Main()
        {
            var options = new JsonSerializerOptions { Converters = { new JsonStringEnumConverter(allowIntegerValues: false) } };
            Console.WriteLine(JsonSerializer.Deserialize<MyEnum>("\"42\"", options)); // does not fail
            Console.WriteLine(JsonSerializer.Deserialize<MyEnum>("42", options)); // fails as expected
        }
        
        public enum MyEnum
        {
            Value = 1
        }
    }

    I would say we should fix this.

  5. changed the title [-]JsonStringEnumConverter ignores JsonNumberHandling.Strict on deserialization[/-] [+]JsonStringEnumConverter ignores 'allowIntegerValues' when deserializing quoted numeric values[/+] on Aug 27, 2021
  6. removed
    untriaged
    on Aug 27, 2021
  7. added this to the 7.0.0 milestone on Aug 27, 2021
  8. added
    Priority:3
    on Aug 27, 2021
  9. wsugarman commented on Sep 27, 2021

    @wsugarman
    Contributor

    Back to the original question, I think allowing integers encoded as strings violates the spirit of this parameter. For example, developers may use JsonStringEnumConverter in their ASP.NET Core projects such their users can specify enumeration names in the request body. This is great because the names are much more description than using numbers. However, I think developers may also assume that they've also decoupled their enum integral values from their web APIs, but this isn't the case. A user could still pass the integral values as strings and successfully bind to the model.

    I understand that accepting both the string and integral representations has precedence with use cases like Enum.TryParse, but personally I would like the option to impose more strictness on my JSON parsing. It would be a welcomed addition if this was provided out-of-the-box.

  10. modified the milestones: 7.0.0, Future on Apr 18, 2022
  11. eiriktsarpalis commented on Apr 18, 2022

    @eiriktsarpalis
    Member

    Moving to Future, as we won't have time to work on this in the .NET 7 timeframe.

  12. 1 remaining item

  13. added
    help wanted
    on Dec 2, 2022
  14. ghost added
    in-pr
    on Dec 9, 2022
  15. davisnw commented on Dec 20, 2022

    @davisnw

    As of 2022-12-19, the documentation at https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonstringenumconverter.-ctor?view=net-7.0#system-text-json-serialization-jsonstringenumconverter-ctor(system-text-json-jsonnamingpolicy-system-boolean) states:

    allowIntegerValues Boolean

    true to allow undefined enum values; otherwise, false. When true, if an enum value isn't defined, it will output as a number rather than a string.

    This clearly implies that you should never receive undefined enum values when parsing with allowIntegerValues: false.

    I could see this discrepancy leading to security vulnerabilities in some api usage contexts.

    (Note: for some reason the links to file feedback on the documentation via github is not available for that page)

  16. ghost removed
    in-pr
    on Mar 6, 2023
  17. ghost added
    in-pr
    on Mar 6, 2023
  18. ghost removed
    in-pr
    on Oct 6, 2023
  19. added
    breaking-change
    on Oct 6, 2023
  20. modified the milestones: Future, 9.0.0 on Oct 6, 2023
  21. ghost added
    needs-breaking-change-doc-created
    on Oct 6, 2023
  22. eiriktsarpalis commented on Oct 6, 2023

    @eiriktsarpalis
    Member

    The fix is technically a breaking change since disabling integers no longer accepts strings containing integers. The workaround is trivial (just enable integers) but we should still document it.

  23. ghost locked as resolved and limited conversation to collaborators on Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:3area-System.Text.Jsonbreaking-changebughelp wantedneeds-breaking-change-doc-created

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions