Skip to content

Commit 6b788bb

Browse files
committed
wta[ksdfdasf
1 parent 071d425 commit 6b788bb

17 files changed

+865
-1489
lines changed

src/JsonApiDotNetCore.OpenApi.Client/ApiException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using JetBrains.Annotations;
44

55
// ReSharper disable once CheckNamespace
6-
namespace OpenApiClientTests.Exceptions
6+
namespace JsonApiDotNetCore.OpenApi.Client.Exceptions
77
{
88
// We cannot rely on a generated ApiException as soon as we are generating multiple clients, see https://github.com/RicoSuter/NSwag/issues/2839#issuecomment-776647377.
99
[UsedImplicitly(ImplicitUseTargetFlags.Members)]

src/JsonApiDotNetCore.OpenApi/JsonApiSchemaIdSelector.cs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,38 @@ internal sealed class JsonApiSchemaIdSelector
1414
{
1515
private static readonly IDictionary<Type, string> OpenTypeToSchemaTemplateMap = new Dictionary<Type, string>
1616
{
17-
[typeof(ResourcePostRequestDocument<>)] = "[Resource] Post Request Document",
18-
[typeof(ResourcePatchRequestDocument<>)] = "[Resource] Patch Request Document",
19-
[typeof(ResourceObjectInPostRequest<>)] = "[Resource] Data In Post Request",
20-
[typeof(AttributesInPostRequest<>)] = "[Resource] Attributes In Post Request",
21-
[typeof(RelationshipsInPostRequest<>)] = "[Resource] Relationships In Post Request",
22-
[typeof(ResourceObjectInPatchRequest<>)] = "[Resource] Data In Patch Request",
23-
[typeof(AttributesInPatchRequest<>)] = "[Resource] Attributes In Patch Request",
24-
[typeof(RelationshipsInPatchRequest<>)] = "[Resource] Relationships In Patch Request",
25-
[typeof(ToOneRelationshipInRequest<>)] = "To One [Resource] In Request",
26-
[typeof(NullableToOneRelationshipInRequest<>)] = "nullable To One [Resource] In Request",
27-
[typeof(ToManyRelationshipInRequest<>)] = "To Many [Resource] In Request",
28-
[typeof(PrimaryResourceResponseDocument<>)] = "[Resource] Primary Response Document",
29-
[typeof(SecondaryResourceResponseDocument<>)] = "[Resource] Secondary Response Document",
30-
[typeof(NullableSecondaryResourceResponseDocument<>)] = "nullable [Resource] Secondary Response Document",
31-
[typeof(ResourceCollectionResponseDocument<>)] = "[Resource] Collection Response Document",
32-
[typeof(ResourceIdentifierResponseDocument<>)] = "[Resource] Identifier Response Document",
33-
[typeof(NullableResourceIdentifierResponseDocument<>)] = "nullable [Resource] Identifier Response Document",
34-
[typeof(ResourceIdentifierCollectionResponseDocument<>)] = "[Resource] Identifier Collection Response Document",
35-
[typeof(ToOneRelationshipInResponse<>)] = "To One [Resource] In Response",
36-
[typeof(NullableToOneRelationshipInResponse<>)] = "nullable To One [Resource] In Response",
37-
[typeof(ToManyRelationshipInResponse<>)] = "To Many [Resource] In Response",
38-
[typeof(ResourceObjectInResponse<>)] = "[Resource] Data In Response",
39-
[typeof(AttributesInResponse<>)] = "[Resource] Attributes In Response",
40-
[typeof(RelationshipsInResponse<>)] = "[Resource] Relationships In Response",
41-
[typeof(ResourceIdentifierObject<>)] = "[Resource] Identifier"
17+
[typeof(ResourcePostRequestDocument<>)] = "[ResourceName] Post Request Document",
18+
[typeof(ResourcePatchRequestDocument<>)] = "[ResourceName] Patch Request Document",
19+
[typeof(ResourceObjectInPostRequest<>)] = "[ResourceName] Data In Post Request",
20+
[typeof(AttributesInPostRequest<>)] = "[ResourceName] Attributes In Post Request",
21+
[typeof(RelationshipsInPostRequest<>)] = "[ResourceName] Relationships In Post Request",
22+
[typeof(ResourceObjectInPatchRequest<>)] = "[ResourceName] Data In Patch Request",
23+
[typeof(AttributesInPatchRequest<>)] = "[ResourceName] Attributes In Patch Request",
24+
[typeof(RelationshipsInPatchRequest<>)] = "[ResourceName] Relationships In Patch Request",
25+
[typeof(ToOneRelationshipInRequest<>)] = "To One [ResourceName] In Request",
26+
[typeof(NullableToOneRelationshipInRequest<>)] = "Nullable To One [ResourceName] In Request",
27+
[typeof(ToManyRelationshipInRequest<>)] = "To Many [ResourceName] In Request",
28+
[typeof(PrimaryResourceResponseDocument<>)] = "[ResourceName] Primary Response Document",
29+
[typeof(SecondaryResourceResponseDocument<>)] = "[ResourceName] Secondary Response Document",
30+
[typeof(NullableSecondaryResourceResponseDocument<>)] = "Nullable [ResourceName] Secondary Response Document",
31+
[typeof(ResourceCollectionResponseDocument<>)] = "[ResourceName] Collection Response Document",
32+
[typeof(ResourceIdentifierResponseDocument<>)] = "[ResourceName] Identifier Response Document",
33+
[typeof(NullableResourceIdentifierResponseDocument<>)] = "Nullable [ResourceName] Identifier Response Document",
34+
[typeof(ResourceIdentifierCollectionResponseDocument<>)] = "[ResourceName] Identifier Collection Response Document",
35+
[typeof(ToOneRelationshipInResponse<>)] = "To One [ResourceName] In Response",
36+
[typeof(NullableToOneRelationshipInResponse<>)] = "Nullable To One [ResourceName] In Response",
37+
[typeof(ToManyRelationshipInResponse<>)] = "To Many [ResourceName] In Response",
38+
[typeof(ResourceObjectInResponse<>)] = "[ResourceName] Data In Response",
39+
[typeof(AttributesInResponse<>)] = "[ResourceName] Attributes In Response",
40+
[typeof(RelationshipsInResponse<>)] = "[ResourceName] Relationships In Response",
41+
[typeof(ResourceIdentifierObject<>)] = "[ResourceName] Identifier"
4242
};
4343

44-
private readonly JsonNamingPolicy _namingPolicy;
44+
private readonly JsonNamingPolicy? _namingPolicy;
4545
private readonly IResourceGraph _resourceGraph;
4646

47-
public JsonApiSchemaIdSelector(JsonNamingPolicy namingPolicy, IResourceGraph resourceGraph)
47+
public JsonApiSchemaIdSelector(JsonNamingPolicy? namingPolicy, IResourceGraph resourceGraph)
4848
{
49-
ArgumentGuard.NotNull(namingPolicy, nameof(namingPolicy));
5049
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));
5150

5251
_namingPolicy = namingPolicy;
@@ -67,28 +66,23 @@ public string GetSchemaId(Type type)
6766
if (type.IsConstructedGenericType && OpenTypeToSchemaTemplateMap.ContainsKey(type.GetGenericTypeDefinition()))
6867
{
6968
string pascalCaseSchemaIdTemplate = OpenTypeToSchemaTemplateMap[type.GetGenericTypeDefinition()];
70-
Type resourceType = type.GetGenericArguments().First();
69+
Type resourceClrType = type.GetGenericArguments().First();
7170

7271
// @formatter:wrap_chained_method_calls chop_always
7372
// @formatter:keep_existing_linebreaks true
7473

7574
string pascalCaseSchemaId = pascalCaseSchemaIdTemplate
76-
.Replace("[ResourceName]", resourceType.Name)
75+
.Replace("[ResourceName]", resourceClrType.Name)
7776
.Replace(" ", "");
7877

7978
// @formatter:keep_existing_linebreaks restore
8079
// @formatter:wrap_chained_method_calls restore
8180

82-
return _namingPolicy.ConvertName(pascalCaseSchemaId);
81+
return _namingPolicy != null ? _namingPolicy.ConvertName(pascalCaseSchemaId) : pascalCaseSchemaId;
8382
}
8483

8584
// Used for a fixed set of types, such as jsonapi-object, links-in-many-resource-document etc.
86-
return _namingPolicy.ConvertName(type.Name);
87-
}
88-
89-
private static string Capitalize(string term)
90-
{
91-
return string.Concat(term[0].ToString().ToUpper(), term.AsSpan(1));
85+
return _namingPolicy != null ? _namingPolicy.ConvertName(type.Name) : type.Name;
9286
}
9387
}
9488
}

src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ private static void AddSwaggerGenerator(IServiceScope scope, IServiceCollection
5959
var resourceGraph = scope.ServiceProvider.GetRequiredService<IResourceGraph>();
6060
var jsonApiOptions = scope.ServiceProvider.GetRequiredService<IJsonApiOptions>();
6161
JsonNamingPolicy? namingPolicy = jsonApiOptions.SerializerOptions.PropertyNamingPolicy;
62-
ResourceNameFormatter resourceNameFormatter = new(namingPolicy);
6362

6463
AddSchemaGenerator(services);
6564

6665
services.AddSwaggerGen(swaggerGenOptions =>
6766
{
6867
swaggerGenOptions.SupportNonNullableReferenceTypes();
6968
SetOperationInfo(swaggerGenOptions, controllerResourceMapping, namingPolicy);
70-
SetSchemaIdSelector(swaggerGenOptions, resourceGraph, resourceNameFormatter);
69+
SetSchemaIdSelector(swaggerGenOptions, resourceGraph, namingPolicy);
7170
swaggerGenOptions.DocumentFilter<EndpointOrderingFilter>();
7271

7372
setupSwaggerGenAction?.Invoke(swaggerGenOptions);
@@ -105,7 +104,7 @@ private static IList<string> GetOperationTags(ApiDescription description, IContr
105104
};
106105
}
107106

108-
private static void SetSchemaIdSelector(SwaggerGenOptions swaggerGenOptions, IResourceGraph resourceGraph, ResourceNameFormatter resourceNameFormatter)
107+
private static void SetSchemaIdSelector(SwaggerGenOptions swaggerGenOptions, IResourceGraph resourceGraph, JsonNamingPolicy? namingPolicy)
109108
{
110109
JsonApiSchemaIdSelector jsonApiObjectSchemaSelector = new(namingPolicy, resourceGraph);
111110

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel.DataAnnotations;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Text.Json;
67
using JsonApiDotNetCore.OpenApi.JsonApiObjects;
78
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Relationships;
89
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
@@ -25,37 +26,32 @@ internal sealed class ResourceFieldObjectSchemaBuilder
2526
private readonly ISchemaRepositoryAccessor _schemaRepositoryAccessor;
2627
private readonly SchemaGenerator _defaultSchemaGenerator;
2728
private readonly ResourceTypeSchemaGenerator _resourceTypeSchemaGenerator;
28-
private readonly SchemaRepository _resourceSchemaRepository;
29+
private readonly SchemaRepository _resourceSchemaRepository = new();
2930
private readonly NullableReferenceSchemaGenerator _nullableReferenceSchemaGenerator;
3031
private readonly IDictionary<string, OpenApiSchema> _schemasForResourceFields;
3132

3233
public ResourceFieldObjectSchemaBuilder(ResourceTypeInfo resourceTypeInfo, ISchemaRepositoryAccessor schemaRepositoryAccessor,
33-
SchemaGenerator defaultSchemaGenerator, JsonApiSchemaIdSelector jsonApiSchemaIdSelector, ResourceTypeSchemaGenerator resourceTypeSchemaGenerator,
34-
SchemaRepository resourceSchemaRepository, JsonNamingPolicy namingPolicy)
34+
SchemaGenerator defaultSchemaGenerator, ResourceTypeSchemaGenerator resourceTypeSchemaGenerator, JsonNamingPolicy? namingPolicy)
3535
{
3636
ArgumentGuard.NotNull(resourceTypeInfo, nameof(resourceTypeInfo));
3737
ArgumentGuard.NotNull(schemaRepositoryAccessor, nameof(schemaRepositoryAccessor));
3838
ArgumentGuard.NotNull(defaultSchemaGenerator, nameof(defaultSchemaGenerator));
39-
ArgumentGuard.NotNull(jsonApiSchemaIdSelector, nameof(jsonApiSchemaIdSelector));
4039
ArgumentGuard.NotNull(resourceTypeSchemaGenerator, nameof(resourceTypeSchemaGenerator));
41-
ArgumentGuard.NotNull(resourceSchemaRepository, nameof(resourceSchemaRepository));
4240

4341
_resourceTypeInfo = resourceTypeInfo;
4442
_schemaRepositoryAccessor = schemaRepositoryAccessor;
4543
_defaultSchemaGenerator = defaultSchemaGenerator;
46-
_jsonApiSchemaIdSelector = jsonApiSchemaIdSelector;
4744
_resourceTypeSchemaGenerator = resourceTypeSchemaGenerator;
48-
_resourceSchemaRepository = resourceSchemaRepository;
4945

5046
_nullableReferenceSchemaGenerator = new NullableReferenceSchemaGenerator(schemaRepositoryAccessor, namingPolicy);
5147
_schemasForResourceFields = GetFieldSchemas();
5248
}
5349

5450
private IDictionary<string, OpenApiSchema> GetFieldSchemas()
5551
{
56-
if (!_resourceSchemaRepository.TryLookupByType(_resourceTypeInfo.ResourceType, out OpenApiSchema referenceSchemaForResource))
52+
if (!_resourceSchemaRepository.TryLookupByType(_resourceTypeInfo.ResourceType.ClrType, out OpenApiSchema referenceSchemaForResource))
5753
{
58-
referenceSchemaForResource = _defaultSchemaGenerator.GenerateSchema(_resourceTypeInfo.ResourceType, _resourceSchemaRepository);
54+
referenceSchemaForResource = _defaultSchemaGenerator.GenerateSchema(_resourceTypeInfo.ResourceType.ClrType, _resourceSchemaRepository);
5955
}
6056

6157
OpenApiSchema fullSchemaForResource = _resourceSchemaRepository.Schemas[referenceSchemaForResource.Reference.Id];

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceObjectSchemaGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IRe
3333
_allowClientGeneratedIds = options.AllowClientGeneratedIds;
3434

3535
_resourceFieldObjectSchemaBuilderFactory = resourceTypeInfo => new ResourceFieldObjectSchemaBuilder(resourceTypeInfo, schemaRepositoryAccessor,
36-
defaultSchemaGenerator, _resourceTypeSchemaGenerator);
36+
defaultSchemaGenerator, _resourceTypeSchemaGenerator, options.SerializerOptions.PropertyNamingPolicy);
3737
}
3838

3939
public OpenApiSchema GenerateSchema(Type resourceObjectType)

0 commit comments

Comments
 (0)