Skip to content

Commit

Permalink
Post-merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Koelman committed Sep 18, 2021
1 parent 831761f commit 8d94ec9
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbuckleVersion)" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="$(SwashbuckleVersion)" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="$(SwashbuckleVersion)" />
</ItemGroup>
</Project>
14 changes: 7 additions & 7 deletions src/JsonApiDotNetCore.OpenApi/JsonApiOperationIdSelector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Humanizer;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
Expand All @@ -9,7 +10,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Newtonsoft.Json.Serialization;

namespace JsonApiDotNetCore.OpenApi
{
Expand All @@ -35,17 +35,17 @@ internal sealed class JsonApiOperationIdSelector
};

private readonly IControllerResourceMapping _controllerResourceMapping;
private readonly NamingStrategy _namingStrategy;
private readonly JsonNamingPolicy _namingPolicy;
private readonly ResourceNameFormatter _formatter;

public JsonApiOperationIdSelector(IControllerResourceMapping controllerResourceMapping, NamingStrategy namingStrategy)
public JsonApiOperationIdSelector(IControllerResourceMapping controllerResourceMapping, JsonNamingPolicy namingPolicy)
{
ArgumentGuard.NotNull(controllerResourceMapping, nameof(controllerResourceMapping));
ArgumentGuard.NotNull(namingStrategy, nameof(namingStrategy));
ArgumentGuard.NotNull(namingPolicy, nameof(namingPolicy));

_controllerResourceMapping = controllerResourceMapping;
_namingStrategy = namingStrategy;
_formatter = new ResourceNameFormatter(namingStrategy);
_namingPolicy = namingPolicy;
_formatter = new ResourceNameFormatter(namingPolicy);
}

public string GetOperationId(ApiDescription endpoint)
Expand Down Expand Up @@ -109,7 +109,7 @@ private string ApplyTemplate(string operationIdTemplate, Type primaryResourceTyp
// @formatter:keep_existing_linebreaks restore
// @formatter:wrap_chained_method_calls restore

return _namingStrategy.GetPropertyName(pascalCaseId, false);
return _namingPolicy.ConvertName(pascalCaseId);
}
}
}
10 changes: 5 additions & 5 deletions src/JsonApiDotNetCore.OpenApi/JsonApiSchemaIdSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ internal sealed class JsonApiSchemaIdSelector
};

private readonly ResourceNameFormatter _formatter;
private readonly IResourceContextProvider _resourceContextProvider;
private readonly IResourceGraph _resourceGraph;

public JsonApiSchemaIdSelector(ResourceNameFormatter formatter, IResourceContextProvider resourceContextProvider)
public JsonApiSchemaIdSelector(ResourceNameFormatter formatter, IResourceGraph resourceGraph)
{
ArgumentGuard.NotNull(formatter, nameof(formatter));
ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));

_formatter = formatter;
_resourceContextProvider = resourceContextProvider;
_resourceGraph = resourceGraph;
}

public string GetSchemaId(Type type)
{
ArgumentGuard.NotNull(type, nameof(type));

ResourceContext resourceContext = _resourceContextProvider.GetResourceContext(type);
ResourceContext resourceContext = _resourceGraph.GetResourceContext(type);

if (resourceContext != null)
{
Expand Down
10 changes: 5 additions & 5 deletions src/JsonApiDotNetCore.OpenApi/OpenApiEndpointConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ namespace JsonApiDotNetCore.OpenApi
/// </summary>
internal sealed class OpenApiEndpointConvention : IActionModelConvention
{
private readonly IResourceContextProvider _resourceContextProvider;
private readonly IResourceGraph _resourceGraph;
private readonly IControllerResourceMapping _controllerResourceMapping;
private readonly EndpointResolver _endpointResolver = new();

public OpenApiEndpointConvention(IResourceContextProvider resourceContextProvider, IControllerResourceMapping controllerResourceMapping)
public OpenApiEndpointConvention(IResourceGraph resourceGraph, IControllerResourceMapping controllerResourceMapping)
{
ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));
ArgumentGuard.NotNull(controllerResourceMapping, nameof(controllerResourceMapping));

_resourceContextProvider = resourceContextProvider;
_resourceGraph = resourceGraph;
_controllerResourceMapping = controllerResourceMapping;
}

Expand Down Expand Up @@ -71,7 +71,7 @@ private IReadOnlyCollection<RelationshipAttribute> GetRelationshipsOfPrimaryReso
{
Type primaryResourceOfEndpointType = _controllerResourceMapping.GetResourceTypeForController(controllerType);

ResourceContext primaryResourceContext = _resourceContextProvider.GetResourceContext(primaryResourceOfEndpointType);
ResourceContext primaryResourceContext = _resourceGraph.GetResourceContext(primaryResourceOfEndpointType);

return primaryResourceContext.Relationships;
}
Expand Down
32 changes: 16 additions & 16 deletions src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.OpenApi.SwaggerComponents;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

Expand Down Expand Up @@ -57,16 +57,16 @@ private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBu
private static void AddSwaggerGenerator(IServiceScope scope, IServiceCollection services, Action<SwaggerGenOptions> setupSwaggerGenAction)
{
var controllerResourceMapping = scope.ServiceProvider.GetRequiredService<IControllerResourceMapping>();
var resourceContextProvider = scope.ServiceProvider.GetRequiredService<IResourceContextProvider>();
var resourceGraph = scope.ServiceProvider.GetRequiredService<IResourceGraph>();
var jsonApiOptions = scope.ServiceProvider.GetRequiredService<IJsonApiOptions>();
NamingStrategy namingStrategy = ((DefaultContractResolver)jsonApiOptions.SerializerSettings.ContractResolver)!.NamingStrategy;
var namingPolicy = jsonApiOptions.SerializerOptions.PropertyNamingPolicy;

AddSchemaGenerator(services);

services.AddSwaggerGen(swaggerGenOptions =>
{
SetOperationInfo(swaggerGenOptions, controllerResourceMapping, resourceContextProvider, namingStrategy);
SetSchemaIdSelector(swaggerGenOptions, resourceContextProvider, namingStrategy);
SetOperationInfo(swaggerGenOptions, controllerResourceMapping, resourceGraph, namingPolicy);
SetSchemaIdSelector(swaggerGenOptions, resourceGraph, namingPolicy);
swaggerGenOptions.DocumentFilter<EndpointOrderingFilter>();
setupSwaggerGenAction?.Invoke(swaggerGenOptions);
Expand All @@ -80,32 +80,32 @@ private static void AddSchemaGenerator(IServiceCollection services)
}

private static void SetOperationInfo(SwaggerGenOptions swaggerGenOptions, IControllerResourceMapping controllerResourceMapping,
IResourceContextProvider resourceContextProvider, NamingStrategy namingStrategy)
IResourceGraph resourceGraph, JsonNamingPolicy namingPolicy)
{
swaggerGenOptions.TagActionsBy(description => GetOperationTags(description, controllerResourceMapping, resourceContextProvider));
swaggerGenOptions.TagActionsBy(description => GetOperationTags(description, controllerResourceMapping, resourceGraph));

JsonApiOperationIdSelector jsonApiOperationIdSelector = new(controllerResourceMapping, namingStrategy);
JsonApiOperationIdSelector jsonApiOperationIdSelector = new(controllerResourceMapping, namingPolicy);
swaggerGenOptions.CustomOperationIds(jsonApiOperationIdSelector.GetOperationId);
}

private static IList<string> GetOperationTags(ApiDescription description, IControllerResourceMapping controllerResourceMapping,
IResourceContextProvider resourceContextProvider)
IResourceGraph resourceGraph)
{
MethodInfo actionMethod = description.ActionDescriptor.GetActionMethod();
Type resourceType = controllerResourceMapping.GetResourceTypeForController(actionMethod.ReflectedType);
ResourceContext resourceContext = resourceContextProvider.GetResourceContext(resourceType);
ResourceContext resourceContext = resourceGraph.GetResourceContext(resourceType);

return new[]
{
resourceContext.PublicName
};
}

private static void SetSchemaIdSelector(SwaggerGenOptions swaggerGenOptions, IResourceContextProvider resourceContextProvider,
NamingStrategy namingStrategy)
private static void SetSchemaIdSelector(SwaggerGenOptions swaggerGenOptions, IResourceGraph resourceGraph,
JsonNamingPolicy namingPolicy)
{
ResourceNameFormatter resourceNameFormatter = new(namingStrategy);
JsonApiSchemaIdSelector jsonApiObjectSchemaSelector = new(resourceNameFormatter, resourceContextProvider);
ResourceNameFormatter resourceNameFormatter = new(namingPolicy);
JsonApiSchemaIdSelector jsonApiObjectSchemaSelector = new(resourceNameFormatter, resourceGraph);

swaggerGenOptions.CustomSchemaIds(type => jsonApiObjectSchemaSelector.GetSchemaId(type));
}
Expand All @@ -127,10 +127,10 @@ private static void AddSwashbuckleCliCompatibility(IServiceScope scope, IMvcCore

private static void AddOpenApiEndpointConvention(IServiceScope scope, IMvcCoreBuilder mvcBuilder)
{
var resourceContextProvider = scope.ServiceProvider.GetRequiredService<IResourceContextProvider>();
var resourceGraph = scope.ServiceProvider.GetRequiredService<IResourceGraph>();
var controllerResourceMapping = scope.ServiceProvider.GetRequiredService<IControllerResourceMapping>();

mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new OpenApiEndpointConvention(resourceContextProvider, controllerResourceMapping)));
mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new OpenApiEndpointConvention(resourceGraph, controllerResourceMapping)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Newtonsoft;
using Swashbuckle.AspNetCore.SwaggerGen;

Expand All @@ -17,17 +16,17 @@ namespace JsonApiDotNetCore.OpenApi.SwaggerComponents
internal sealed class JsonApiDataContractResolver : ISerializerDataContractResolver
{
private readonly NewtonsoftDataContractResolver _dataContractResolver;
private readonly IResourceContextProvider _resourceContextProvider;
private readonly IResourceGraph _resourceGraph;

public JsonApiDataContractResolver(IResourceContextProvider resourceContextProvider, IJsonApiOptions jsonApiOptions)
public JsonApiDataContractResolver(IResourceGraph resourceGraph, IJsonApiOptions jsonApiOptions)
{
ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));
ArgumentGuard.NotNull(jsonApiOptions, nameof(jsonApiOptions));

_resourceContextProvider = resourceContextProvider;
_resourceGraph = resourceGraph;

JsonSerializerSettings serializerSettings = jsonApiOptions.SerializerSettings ?? new JsonSerializerSettings();
_dataContractResolver = new NewtonsoftDataContractResolver(serializerSettings);
var serializerOptions = jsonApiOptions.SerializerOptions;
_dataContractResolver = new NewtonsoftDataContractResolver(serializerOptions);
}

public DataContract GetDataContractForType(Type type)
Expand Down Expand Up @@ -65,7 +64,7 @@ private static DataContract ReplacePropertiesInDataContract(DataContract dataCon

private IList<DataProperty> GetDataPropertiesThatExistInResourceContext(Type resourceType, DataContract dataContract)
{
ResourceContext resourceContext = _resourceContextProvider.GetResourceContext(resourceType);
ResourceContext resourceContext = _resourceGraph.GetResourceContext(resourceType);
var dataProperties = new List<DataProperty>();

foreach (DataProperty property in dataContract.ObjectProperties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ internal sealed class JsonApiSchemaGenerator : ISchemaGenerator
private readonly JsonApiObjectNullabilityProcessor _jsonApiObjectNullabilityProcessor;
private readonly SchemaRepositoryAccessor _schemaRepositoryAccessor = new();

public JsonApiSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceContextProvider resourceContextProvider, IJsonApiOptions jsonApiOptions)
public JsonApiSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceGraph resourceGraph, IJsonApiOptions jsonApiOptions)
{
ArgumentGuard.NotNull(defaultSchemaGenerator, nameof(defaultSchemaGenerator));
ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));
ArgumentGuard.NotNull(jsonApiOptions, nameof(jsonApiOptions));

_defaultSchemaGenerator = defaultSchemaGenerator;
_nullableReferenceSchemaGenerator = new NullableReferenceSchemaGenerator(_schemaRepositoryAccessor);
_jsonApiObjectNullabilityProcessor = new JsonApiObjectNullabilityProcessor(_schemaRepositoryAccessor);

_resourceObjectSchemaGenerator =
new ResourceObjectSchemaGenerator(defaultSchemaGenerator, resourceContextProvider, jsonApiOptions, _schemaRepositoryAccessor);
new ResourceObjectSchemaGenerator(defaultSchemaGenerator, resourceGraph, jsonApiOptions, _schemaRepositoryAccessor);
}

public OpenApiSchema GenerateSchema(Type type, SchemaRepository schemaRepository, MemberInfo memberInfo = null, ParameterInfo parameterInfo = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,45 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace JsonApiDotNetCore.OpenApi.SwaggerComponents
{
internal sealed class ResourceObjectSchemaGenerator
{
private readonly SchemaGenerator _defaultSchemaGenerator;
private readonly IResourceContextProvider _resourceContextProvider;
private readonly IResourceGraph _resourceGraph;
private readonly ISchemaRepositoryAccessor _schemaRepositoryAccessor;
private readonly ResourceTypeSchemaGenerator _resourceTypeSchemaGenerator;
private readonly bool _allowClientGeneratedIds;
private readonly Func<ResourceTypeInfo, ResourceFieldObjectSchemaBuilder> _createFieldObjectBuilderFactory;

public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceContextProvider resourceContextProvider,
public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceGraph resourceGraph,
IJsonApiOptions jsonApiOptions, ISchemaRepositoryAccessor schemaRepositoryAccessor)
{
ArgumentGuard.NotNull(defaultSchemaGenerator, nameof(defaultSchemaGenerator));
ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));
ArgumentGuard.NotNull(jsonApiOptions, nameof(jsonApiOptions));
ArgumentGuard.NotNull(schemaRepositoryAccessor, nameof(schemaRepositoryAccessor));

_defaultSchemaGenerator = defaultSchemaGenerator;
_resourceContextProvider = resourceContextProvider;
_resourceGraph = resourceGraph;
_schemaRepositoryAccessor = schemaRepositoryAccessor;

_resourceTypeSchemaGenerator = new ResourceTypeSchemaGenerator(schemaRepositoryAccessor, resourceContextProvider);
_resourceTypeSchemaGenerator = new ResourceTypeSchemaGenerator(schemaRepositoryAccessor, resourceGraph);
_allowClientGeneratedIds = jsonApiOptions.AllowClientGeneratedIds;

_createFieldObjectBuilderFactory = CreateFieldObjectBuilderFactory(defaultSchemaGenerator, resourceContextProvider, jsonApiOptions,
_createFieldObjectBuilderFactory = CreateFieldObjectBuilderFactory(defaultSchemaGenerator, resourceGraph, jsonApiOptions,
schemaRepositoryAccessor, _resourceTypeSchemaGenerator);
}

private static Func<ResourceTypeInfo, ResourceFieldObjectSchemaBuilder> CreateFieldObjectBuilderFactory(SchemaGenerator defaultSchemaGenerator,
IResourceContextProvider resourceContextProvider, IJsonApiOptions jsonApiOptions, ISchemaRepositoryAccessor schemaRepositoryAccessor,
IResourceGraph resourceGraph, IJsonApiOptions jsonApiOptions, ISchemaRepositoryAccessor schemaRepositoryAccessor,
ResourceTypeSchemaGenerator resourceTypeSchemaGenerator)
{
NamingStrategy namingStrategy = ((DefaultContractResolver)jsonApiOptions.SerializerSettings.ContractResolver)!.NamingStrategy;
ResourceNameFormatter resourceNameFormatter = new(namingStrategy);
var jsonApiSchemaIdSelector = new JsonApiSchemaIdSelector(resourceNameFormatter, resourceContextProvider);
var namingPolicy = jsonApiOptions.SerializerOptions.PropertyNamingPolicy;
ResourceNameFormatter resourceNameFormatter = new(namingPolicy);
var jsonApiSchemaIdSelector = new JsonApiSchemaIdSelector(resourceNameFormatter, resourceGraph);

return resourceTypeInfo => new ResourceFieldObjectSchemaBuilder(resourceTypeInfo, schemaRepositoryAccessor, defaultSchemaGenerator,
jsonApiSchemaIdSelector, resourceTypeSchemaGenerator);
Expand All @@ -54,7 +53,7 @@ public OpenApiSchema GenerateSchema(Type resourceObjectType)

(OpenApiSchema fullSchemaForResourceObject, OpenApiSchema referenceSchemaForResourceObject) = EnsureSchemasExist(resourceObjectType);

var resourceTypeInfo = ResourceTypeInfo.Create(resourceObjectType, _resourceContextProvider);
var resourceTypeInfo = ResourceTypeInfo.Create(resourceObjectType, _resourceGraph);
ResourceFieldObjectSchemaBuilder fieldObjectBuilder = _createFieldObjectBuilderFactory(resourceTypeInfo);

RemoveResourceIdIfPostResourceObject(resourceTypeInfo.ResourceObjectOpenType, fullSchemaForResourceObject);
Expand Down
Loading

0 comments on commit 8d94ec9

Please sign in to comment.