Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataMember(IsRequired = true)] do not mark property as required #2383

Closed
ouvreboite opened this issue Mar 23, 2022 · 3 comments · Fixed by #2644
Closed

[DataMember(IsRequired = true)] do not mark property as required #2383

ouvreboite opened this issue Mar 23, 2022 · 3 comments · Fixed by #2644
Milestone

Comments

@ouvreboite
Copy link
Contributor

Swashbuckle.AspNetCore.SwaggerGen 6.3.0
Swashbuckle.AspNetCore.Newtonsoft 6.3.0

A field annotated with [DataMember(IsRequired = true)] is not marked as required in the generated OpenApi specification.

// class
[DataContract]
public class DataMemberModel
{
    [DataMember(Name = "customId", IsRequired = true)]
    public string Id;
}

// schema
"DataMemberModel": {
    "type": "object",
    "properties": {
        "customId": {  //the id shows that DataMember is indeed used
            "type": "string",
            "nullable": true
        }
    },
    "additionalProperties": false
}

The "required" status is supposed to be set in NewtonsoftDataContractResolver.GetDataPropertiesFor, using JsonPropertyExtensions.IsRequiredSpecified to know if JsonProperty.Required is readable. But JsonPropertyExtensions.IsRequiredSpecified does not actually look at JsonProperty.Required (which is equals to NotNull in our case). It incorrectly return false.

//NewtonsoftDataContractResolver.cs
var required = jsonProperty.IsRequiredSpecified()
      ? jsonProperty.Required
      : jsonObjectContract.ItemRequired ?? Required.Default;

//JsonPropertyExtensions.cs
public static bool IsRequiredSpecified(this JsonProperty jsonProperty)
{
    if (!jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo))
        return false;

    if (memberInfo.GetCustomAttribute<JsonRequiredAttribute>() != null)
        return true;

    var jsonPropertyAttributeData = memberInfo.GetCustomAttributesData()
        .FirstOrDefault(attrData => attrData.AttributeType == typeof(JsonPropertyAttribute));

    return (jsonPropertyAttributeData != null) && jsonPropertyAttributeData.NamedArguments.Any(arg => arg.MemberName == "Required");
}

Maybe adding a if(jsonProperty.Required != Required.Default) return true at the start of IsRequiredSpecified could be enough.

@martincostello
Copy link
Collaborator

#2644 (comment)

Copy link
Contributor

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.

@github-actions github-actions bot added the stale Stale issues or pull requests label Jun 17, 2024
Copy link
Contributor

github-actions bot commented Jul 5, 2024

This issue was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2024
@martincostello martincostello removed the stale Stale issues or pull requests label Aug 1, 2024
@martincostello martincostello added this to the v6.7.1 milestone Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants