Skip to content

Commit

Permalink
Apply [SwaggerIgnore] for Newtonsoft.Json (#3134)
Browse files Browse the repository at this point in the history
Honour usage of `[SwaggerIgnore]` correctly when using Newtonsoft.Json.
  • Loading branch information
jgarciadelanoceda authored Nov 8, 2024
1 parent c547b28 commit 4440820
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Swashbuckle.AspNetCore.Newtonsoft
Expand Down Expand Up @@ -150,8 +151,11 @@ private List<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonObjectCon

foreach (var jsonProperty in jsonObjectContract.Properties)
{
if (jsonProperty.Ignored) continue;

bool memberInfoIsObtained = jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo);
if (jsonProperty.Ignored || (memberInfoIsObtained && memberInfo.CustomAttributes.Any(t => t.AttributeType == typeof(SwaggerIgnoreAttribute))))
{
continue;
}
var required = jsonProperty.IsRequiredSpecified()
? jsonProperty.Required
: jsonObjectContract.ItemRequired ?? Required.Default;
Expand All @@ -166,8 +170,6 @@ private List<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonObjectCon
string.Equals(p.Name, jsonProperty.PropertyName, StringComparison.OrdinalIgnoreCase);
});

jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo);

dataProperties.Add(
new DataProperty(
name: jsonProperty.PropertyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.TestSupport;
using Xunit;
using Swashbuckle.AspNetCore.SwaggerGen.Test.Fixtures;

namespace Swashbuckle.AspNetCore.Newtonsoft.Test
{
Expand Down Expand Up @@ -828,6 +829,22 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonExtensionData()
Assert.Null(schema.AdditionalProperties.Type);
}

[Fact]
public void GenerateSchema_HonorsAttribute_SwaggerIgnore()
{
var schemaRepository = new SchemaRepository();

var referenceSchema = Subject().GenerateSchema(typeof(SwaggerIngoreAnnotatedType), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];

Assert.True(schema.Properties.ContainsKey(nameof(SwaggerIngoreAnnotatedType.NotIgnoredString)));
Assert.False(schema.Properties.ContainsKey(nameof(SwaggerIngoreAnnotatedType.IgnoredString)));
Assert.False(schema.Properties.ContainsKey(nameof(SwaggerIngoreAnnotatedType.IgnoredExtensionData)));
Assert.False(schema.AdditionalPropertiesAllowed);
Assert.Null(schema.AdditionalProperties);
}

[Fact]
public void GenerateSchema_HonorsDataMemberAttribute()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Swashbuckle.AspNetCore.Newtonsoft\Swashbuckle.AspNetCore.Newtonsoft.csproj" />
<ProjectReference Include="..\Swashbuckle.AspNetCore.SwaggerGen.Test\Swashbuckle.AspNetCore.SwaggerGen.Test.csproj" />
<ProjectReference Include="..\Swashbuckle.AspNetCore.TestSupport\Swashbuckle.AspNetCore.TestSupport.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 4440820

Please sign in to comment.