Skip to content

Annotation Based DefaultValues only works for strings #4220

@SleeplessOne1917

Description

@SleeplessOne1917

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The DefaultValue attribute only works for strings. Take the following class:

public class MyTypeInput
{
  [DefaultValue("foo")]
  public string String1 { get; set; }
  
  [DefaultValue(true)]
  public bool Bool1 { get; set; }
  
  [DefaultValue(42)]
  public int Int1 { get; set; }
  
  [DefaultValue(3.14)]
  public double Float1 { get; set; }

  [DefaultValue(MyEnum.Value1)]
  public MyEnum Enum1 { get; set; }
}

It produces the following SDL:

input MyTypeInput {
  string1: String = "foo"
  bool1: Boolean! = true
  int1: Int! = 42
  float1: Float! = 3.14
  enum1: MyEnum! = VALUE1
}

Note that the Boolean, Int, Float, and MyEnum fields are all non-nullable despite having default values defined. When making a request to the endpoint, the non-null requirement makes it so those fields have to be set in the input, treating them exactly the same as non-null fields without a default argument specified in 11.3.7. It recognizes the defaults in 12.0.0-rc.6.

I also tried this with the following class:

public class MyTypeInput
{
  [DefaultValue("foo")]
  public string String1 { get; set; }
  
  [DefaultValue(true)]
  public bool? Bool1 { get; set; }
  
  [DefaultValue(42)]
  public int? Int1 { get; set; }
  
  [DefaultValue(3.14)]
  public double? Float1 { get; set; }

  [DefaultValue(MyEnum.Value1)]
  public MyEnum? Enum1 { get; set; }
}

Note that this time, the bool, int, double, and MyEnum are all nullable. Nevertheless, the exact same SDL gets produced.

Update: I originally tested this in 11.3.7. I updated my examples to account for a bug fix that wasn't included in that version.

Steps to reproduce

  1. Clone this repository
  2. Build and run the solution
  3. Download the SDL at https://localhost:5001/graphql?sdl (exact url may differ on your machine)
  4. Note that the input type produced has fields that have default values marked as non-null

You can also attempt to hit the graphql endpoint with an editor of your choice. If you attempt to use the returnTypePassedIn query without specifying the bool, float, enum, and int fields, you'll get an error like "The field 'field name' is required and must be set." the query will work in 12.0.0-rc.6, but fail in 11.3.7.

Relevant log output

No response

Additional Context?

No response

Product

Hot Chocolate

Version

12.0.0-rc.6

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions