Skip to content

Commit 41c24c4

Browse files
committed
Replace NewtonsoftDataContractResolver with JsonSerializerDataContractResolver
1 parent 8d94ec9 commit 41c24c4

File tree

7 files changed

+113
-111
lines changed

7 files changed

+113
-111
lines changed

src/JsonApiDotNetCore.OpenApi/JsonApiSchemaIdSelector.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public string GetSchemaId(Type type)
4646
{
4747
ArgumentGuard.NotNull(type, nameof(type));
4848

49-
ResourceContext resourceContext = _resourceGraph.GetResourceContext(type);
49+
ResourceContext resourceContext = _resourceGraph.TryGetResourceContext(type);
5050

5151
if (resourceContext != null)
5252
{

src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static void AddSwaggerGenerator(IServiceScope scope, IServiceCollection
5959
var controllerResourceMapping = scope.ServiceProvider.GetRequiredService<IControllerResourceMapping>();
6060
var resourceGraph = scope.ServiceProvider.GetRequiredService<IResourceGraph>();
6161
var jsonApiOptions = scope.ServiceProvider.GetRequiredService<IJsonApiOptions>();
62-
var namingPolicy = jsonApiOptions.SerializerOptions.PropertyNamingPolicy;
62+
JsonNamingPolicy namingPolicy = jsonApiOptions.SerializerOptions.PropertyNamingPolicy;
6363

6464
AddSchemaGenerator(services);
6565

@@ -101,8 +101,7 @@ private static IList<string> GetOperationTags(ApiDescription description, IContr
101101
};
102102
}
103103

104-
private static void SetSchemaIdSelector(SwaggerGenOptions swaggerGenOptions, IResourceGraph resourceGraph,
105-
JsonNamingPolicy namingPolicy)
104+
private static void SetSchemaIdSelector(SwaggerGenOptions swaggerGenOptions, IResourceGraph resourceGraph, JsonNamingPolicy namingPolicy)
106105
{
107106
ResourceNameFormatter resourceNameFormatter = new(namingPolicy);
108107
JsonApiSchemaIdSelector jsonApiObjectSchemaSelector = new(resourceNameFormatter, resourceGraph);

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/JsonApiDataContractResolver.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Text.Json;
56
using JsonApiDotNetCore.Configuration;
67
using JsonApiDotNetCore.Resources;
78
using JsonApiDotNetCore.Resources.Annotations;
8-
using Swashbuckle.AspNetCore.Newtonsoft;
99
using Swashbuckle.AspNetCore.SwaggerGen;
1010

1111
namespace JsonApiDotNetCore.OpenApi.SwaggerComponents
1212
{
1313
/// <summary>
14-
/// For schema generation, we rely on <see cref="NewtonsoftDataContractResolver" /> from Swashbuckle for all but our own JSON:API types.
14+
/// For schema generation, we rely on <see cref="JsonSerializerDataContractResolver" /> from Swashbuckle for all but our own JSON:API types.
1515
/// </summary>
1616
internal sealed class JsonApiDataContractResolver : ISerializerDataContractResolver
1717
{
18-
private readonly NewtonsoftDataContractResolver _dataContractResolver;
18+
private readonly JsonSerializerDataContractResolver _dataContractResolver;
1919
private readonly IResourceGraph _resourceGraph;
2020

2121
public JsonApiDataContractResolver(IResourceGraph resourceGraph, IJsonApiOptions jsonApiOptions)
@@ -25,8 +25,8 @@ public JsonApiDataContractResolver(IResourceGraph resourceGraph, IJsonApiOptions
2525

2626
_resourceGraph = resourceGraph;
2727

28-
var serializerOptions = jsonApiOptions.SerializerOptions;
29-
_dataContractResolver = new NewtonsoftDataContractResolver(serializerOptions);
28+
JsonSerializerOptions serializerOptions = jsonApiOptions.SerializerOptions;
29+
_dataContractResolver = new JsonSerializerDataContractResolver(serializerOptions);
3030
}
3131

3232
public DataContract GetDataContractForType(Type type)

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceObjectSchemaGenerator.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text.Json;
34
using JsonApiDotNetCore.Configuration;
45
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
56
using Microsoft.OpenApi.Models;
@@ -16,8 +17,8 @@ internal sealed class ResourceObjectSchemaGenerator
1617
private readonly bool _allowClientGeneratedIds;
1718
private readonly Func<ResourceTypeInfo, ResourceFieldObjectSchemaBuilder> _createFieldObjectBuilderFactory;
1819

19-
public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceGraph resourceGraph,
20-
IJsonApiOptions jsonApiOptions, ISchemaRepositoryAccessor schemaRepositoryAccessor)
20+
public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceGraph resourceGraph, IJsonApiOptions jsonApiOptions,
21+
ISchemaRepositoryAccessor schemaRepositoryAccessor)
2122
{
2223
ArgumentGuard.NotNull(defaultSchemaGenerator, nameof(defaultSchemaGenerator));
2324
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));
@@ -31,15 +32,15 @@ public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IRe
3132
_resourceTypeSchemaGenerator = new ResourceTypeSchemaGenerator(schemaRepositoryAccessor, resourceGraph);
3233
_allowClientGeneratedIds = jsonApiOptions.AllowClientGeneratedIds;
3334

34-
_createFieldObjectBuilderFactory = CreateFieldObjectBuilderFactory(defaultSchemaGenerator, resourceGraph, jsonApiOptions,
35-
schemaRepositoryAccessor, _resourceTypeSchemaGenerator);
35+
_createFieldObjectBuilderFactory = CreateFieldObjectBuilderFactory(defaultSchemaGenerator, resourceGraph, jsonApiOptions, schemaRepositoryAccessor,
36+
_resourceTypeSchemaGenerator);
3637
}
3738

3839
private static Func<ResourceTypeInfo, ResourceFieldObjectSchemaBuilder> CreateFieldObjectBuilderFactory(SchemaGenerator defaultSchemaGenerator,
3940
IResourceGraph resourceGraph, IJsonApiOptions jsonApiOptions, ISchemaRepositoryAccessor schemaRepositoryAccessor,
4041
ResourceTypeSchemaGenerator resourceTypeSchemaGenerator)
4142
{
42-
var namingPolicy = jsonApiOptions.SerializerOptions.PropertyNamingPolicy;
43+
JsonNamingPolicy namingPolicy = jsonApiOptions.SerializerOptions.PropertyNamingPolicy;
4344
ResourceNameFormatter resourceNameFormatter = new(namingPolicy);
4445
var jsonApiSchemaIdSelector = new JsonApiSchemaIdSelector(resourceNameFormatter, resourceGraph);
4546

test/OpenApiClientTests/LegacyClient/ResponseTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public async Task Getting_resource_translates_response()
187187
document.Data.Attributes.ServicesOnBoard.Should().BeNull();
188188
document.Data.Attributes.FinalDestination.Should().BeNull();
189189
document.Data.Attributes.StopOverDestination.Should().BeNull();
190-
document.Data.Attributes.OperatedBy.Should().Be(default(Airline));
190+
document.Data.Attributes.OperatedBy.Should().Be(default);
191191
}
192192

193193
[Fact]

test/OpenApiTests/LegacyOpenApiIntegration/LegacyOpenApiIntegrationStartup.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json.Serialization;
12
using JetBrains.Annotations;
23
using JsonApiDotNetCore.Configuration;
34
using JsonApiDotNetCore.Resources.Annotations;
@@ -17,6 +18,7 @@ protected override void SetJsonApiOptions(JsonApiOptions options)
1718
options.DefaultAttrCapabilities = AttrCapabilities.AllowView;
1819
options.SerializerOptions.PropertyNamingPolicy = JsonKebabCaseNamingPolicy.Instance;
1920
options.SerializerOptions.DictionaryKeyPolicy = JsonKebabCaseNamingPolicy.Instance;
21+
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
2022
}
2123
}
2224
}

0 commit comments

Comments
 (0)