Swashbuckle.aspnetcore: Can not hide property

Created on 25 Oct 2017  路  9Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I can not hide a property from the UI.

I wanted do this because my API have some properties that are meant for private use as they are still under development and are to be undocumented and hidden until later when they become a part of the public API as they might change.

    public class Model
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [HiddenInput]
        [IgnoreDataMember]
        [JsonIgnore]
        public string Hidden { get; set; }
    }

Most helpful comment

https://github.com/domaindrivendev/Swashbuckle/issues/178
https://github.com/domaindrivendev/Swashbuckle/issues/1230

Not a single solution posted here in the issues nor on stackoverflow work for the GET endpoints that use [FromQuery] binder.
With ISchemaFilter there are simply no properties to act on - SchemaFilterContext.Properties is empty for such objects.
IDocumentFilter does not contain definitions anymore.

internal, protected, and private work but the objects are generally accessed from other projects and/or from the unit tests.

All 9 comments

JsonIgnore should definitely work. Also setting the access modifier to internal should work too. Basically, Swashbuckle should mirror the JsonSerializer behavior

Closing due to inactivity

Using Swashbuckle 2.3.0.

    public class Model
    {
        public bool Property1 { get; set; }
        internal bool Property2 { get; set; }

        [JsonIgnore]
        public bool Property3 { get; set; }
    }

Property1 and Property3 shows. Property2 is not shown.

So using the internal access modifier works. However when using the same model as entity model instead of a DTO then this also hides it from Entity Framework.

The JsonIgnore attribute does not seem to work.

{
    "/api/Example": {
        "get": {
            "tags": ["Example"],
            "operationId": "ApiExampleGet",
            "consumes": [],
            "produces": ["text/plain", "application/json", "text/json"],
            "parameters": [{
                "name": "Property1",
                "in": "query",
                "required": true,
                "type": "boolean"
            }, {
                "name": "Property3",
                "in": "query",
                "required": true,
                "type": "boolean"
            }],
            "responses": {
                "200": {
                    "description": "Success",
                    "schema": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

Ping @domaindrivendev

I would like to know how to hide property.

+1

[JsonIgnore] and private/internal do not work for me when I use them on a class that has a base class whose public property I'm trying to hide.

Ex:

public class BaseClass
{
    public string Foo { get; set; }
}

public class DerivedClass : BaseClass
{
    // The "Foo" property is still displayed by Swashbuckle
    [JsonIgnore]
    private new string Foo { get; set; }
}

Is this expected? If so, is there a way to hide public properties from base classes without modifying the base class?

Swashbuckle simply honors the built-in serialization behavior. If you try to serialize an instance of DerivedClass you will observe that the Foo property is in fact outputted. Hence, Swashbuckle describes the behavior accurately.

Now ... the next question is why does the built-in serializer behave that way. Perhaps the response here from Mr Newton-King himself will provide some further insight - https://github.com/JamesNK/Newtonsoft.Json/issues/463

That helps. Thanks!

https://github.com/domaindrivendev/Swashbuckle/issues/178
https://github.com/domaindrivendev/Swashbuckle/issues/1230

Not a single solution posted here in the issues nor on stackoverflow work for the GET endpoints that use [FromQuery] binder.
With ISchemaFilter there are simply no properties to act on - SchemaFilterContext.Properties is empty for such objects.
IDocumentFilter does not contain definitions anymore.

internal, protected, and private work but the objects are generally accessed from other projects and/or from the unit tests.

Was this page helpful?
0 / 5 - 0 ratings