Skip to content

Added Swashbuckle.AspNetCore.Annotations.SwaggerIgnoreAttribute#2610

Merged
martincostello merged 6 commits into
domaindrivendev:masterfrom
jcracknell:swaggerignore
Apr 18, 2024
Merged

Added Swashbuckle.AspNetCore.Annotations.SwaggerIgnoreAttribute#2610
martincostello merged 6 commits into
domaindrivendev:masterfrom
jcracknell:swaggerignore

Conversation

@jcracknell

Copy link
Copy Markdown
Contributor

Application of the SwaggerIgnoreAttribute suppresses the annotated property during schema generation, without altering the serialization behavior as with JsonIgnoreAttribute.

This is useful in a scenario where you want to capture and reject unhandled properties, without advertising that you accept additionalProperties (which is the opposite of what is intended):

public abstract class RequestBase : IValidatableObject {
    [SwaggerIgnore]
    [JsonExtensionData]
    public IDictionary<string, JsonElement>? UnsupportedProperties { get; set; }

    public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
        if(UnsupportedProperties is { Count: not 0 })
            foreach(var entry in UnsupportedProperties)
                yield return new ValidationResult($"Unsupported property: {entry.Key}.", new[] { entry.Key });
    }
}

I added the annotation to the Swashbuckle.AspNetCore.SwaggerGen project as it is much easier to filter the properties in the JsonSerializerDataContractResolver than after the fact in a filter, as you would need to regenerate the data contract for the type and then remove all of the annotated properties and derived side effects.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants