Skip to content

Model with Custom Attribute without PropertyName throws error #101

Description

@kylednoland

Repro Steps

Add an OpenApiResponseWithBody on a Function with a response with type that is decorated with a custom attribute without the PropertyName field populated.

Example of Function Attribute

 [OpenApiResponseWithBody(HttpStatusCode.OK, "application/json", typeof(Thing), Summary = "A returned Thing")]

Example of referenced class that produces the error

    public class Thing
    {
        [JsonProperty(Required = Required.Default)]
        public string Text { get; set; }
    }

Currently, the Function will throw an error when generating an OpenAPI document with a response: System.Private.CoreLib: Exception while executing function: RenderSwaggerDocument. System.Private.CoreLib: Value cannot be null. Parameter name: key..

Potential Solution

In PropertyInfoExtensions.cs add a null check for when there is no PropertyName and return the element.Name property instead such as:

        public static string GetJsonPropertyName(this PropertyInfo element)
        {
            if (element.ExistsCustomAttribute<JsonPropertyAttribute>())
            {
                var name = element.GetCustomAttribute<JsonPropertyAttribute>().PropertyName;

                if (name == null)
                {
                    return element.Name;
                }

                return name;

            }

            return element.Name;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions