Skip to content

Commit 22f6d92

Browse files
committed
Various small internal cleanups
1 parent 68cad58 commit 22f6d92

File tree

12 files changed

+41
-32
lines changed

12 files changed

+41
-32
lines changed

docs/usage/openapi-client.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ Our [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/
220220
which seems to work. If you're an MSBuild expert, please help out!
221221

222222
```xml
223-
<Target Name="RemoveKiotaGeneratedCode" BeforeTargets="BeforeCompile;CoreCompile" Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
223+
<Target Name="ExcludeKiotaGeneratedCode" BeforeTargets="BeforeCompile;CoreCompile" Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
224224
<ItemGroup>
225225
<Compile Remove="**\GeneratedCode\**\*.cs" />
226226
</ItemGroup>
227227
</Target>
228228

229-
<Target Name="KiotaRunTool" BeforeTargets="BeforeCompile;CoreCompile" AfterTargets="RemoveKiotaGeneratedCode"
229+
<Target Name="KiotaRunTool" BeforeTargets="BeforeCompile;CoreCompile" AfterTargets="ExcludeKiotaGeneratedCode"
230230
Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
231231
<Exec
232232
Command="dotnet kiota generate --language CSharp --class-name ExampleApiClient --namespace-name OpenApiKiotaClientExample.GeneratedCode --output ./GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --log-level Error --openapi ../JsonApiDotNetCoreExample/GeneratedSwagger/JsonApiDotNetCoreExample.json" />

src/Examples/OpenApiKiotaClientExample/OpenApiKiotaClientExample.csproj

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,14 @@
2020
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="$(KiotaVersion)" />
2121
</ItemGroup>
2222

23-
<Target Name="RemoveKiotaGeneratedCode" BeforeTargets="BeforeCompile;CoreCompile" Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
23+
<Target Name="ExcludeKiotaGeneratedCode" BeforeTargets="BeforeCompile;CoreCompile" Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
2424
<ItemGroup>
2525
<Compile Remove="**\GeneratedCode\**\*.cs" />
2626
</ItemGroup>
2727
</Target>
2828

29-
<Target Name="KiotaRunTool" BeforeTargets="BeforeCompile;CoreCompile" AfterTargets="RemoveKiotaGeneratedCode"
29+
<Target Name="KiotaRunTool" BeforeTargets="BeforeCompile;CoreCompile" AfterTargets="ExcludeKiotaGeneratedCode"
3030
Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
31-
<!--
32-
Created from sources:
33-
- https://github.com/microsoft/kiota/issues/3005#issuecomment-1657806848
34-
- https://github.com/dotnet/project-system/blob/main/docs/design-time-builds.md#determining-whether-a-target-is-running-in-a-design-time-build
35-
- https://learn.microsoft.com/en-us/visualstudio/msbuild/tutorial-custom-task-code-generation?view=vs-2022
36-
- https://gist.github.com/KirillOsenkov/f20cb84d37a89b01db63f8aafe03f19b
37-
-->
3831
<Exec
3932
Command="dotnet kiota generate --language CSharp --class-name ExampleApiClient --namespace-name OpenApiKiotaClientExample.GeneratedCode --output ./GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --log-level Error --openapi ../JsonApiDotNetCoreExample/GeneratedSwagger/JsonApiDotNetCoreExample.json" />
4033
</Target>

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Bodies/AtomicOperationsBodySchemaGenerator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ private void GenerateSchemaForOperation(ResourceType resourceType, SchemaReposit
117117
private void GenerateSchemaForResourceOperation(Type operationOpenType, ResourceType resourceType, AtomicOperationCode operationCode,
118118
SchemaRepository schemaRepository)
119119
{
120-
WriteOperationKind operationKind = operationCode switch
120+
WriteOperationKind writeOperation = operationCode switch
121121
{
122122
AtomicOperationCode.Add => WriteOperationKind.CreateResource,
123123
AtomicOperationCode.Update => WriteOperationKind.UpdateResource,
124124
AtomicOperationCode.Remove => WriteOperationKind.DeleteResource,
125125
_ => throw new UnreachableCodeException()
126126
};
127127

128-
if (!_atomicOperationFilter.IsEnabled(resourceType, operationKind))
128+
if (!_atomicOperationFilter.IsEnabled(resourceType, writeOperation))
129129
{
130130
return;
131131
}
@@ -154,25 +154,25 @@ private void GenerateSchemaForResourceOperation(Type operationOpenType, Resource
154154
private void GenerateSchemaForRelationshipOperation(Type operationOpenType, RelationshipAttribute relationship, AtomicOperationCode operationCode,
155155
SchemaRepository schemaRepository)
156156
{
157-
WriteOperationKind operationKind = operationCode switch
157+
WriteOperationKind writeOperation = operationCode switch
158158
{
159159
AtomicOperationCode.Add => WriteOperationKind.AddToRelationship,
160160
AtomicOperationCode.Update => WriteOperationKind.SetRelationship,
161161
AtomicOperationCode.Remove => WriteOperationKind.RemoveFromRelationship,
162162
_ => throw new UnreachableCodeException()
163163
};
164164

165-
if (!_atomicOperationFilter.IsEnabled(relationship.LeftType, operationKind))
165+
if (!_atomicOperationFilter.IsEnabled(relationship.LeftType, writeOperation))
166166
{
167167
return;
168168
}
169169

170-
if (relationship is HasOneAttribute hasOneRelationship && !IsToOneRelationshipEnabled(hasOneRelationship, operationKind))
170+
if (relationship is HasOneAttribute hasOneRelationship && !IsToOneRelationshipEnabled(hasOneRelationship, writeOperation))
171171
{
172172
return;
173173
}
174174

175-
if (relationship is HasManyAttribute hasManyRelationship && !IsToManyRelationshipEnabled(hasManyRelationship, operationKind))
175+
if (relationship is HasManyAttribute hasManyRelationship && !IsToManyRelationshipEnabled(hasManyRelationship, writeOperation))
176176
{
177177
return;
178178
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Components/DataContainerSchemaGenerator.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public OpenApiSchema GenerateSchema(Type dataContainerConstructedType, ResourceT
3737
ArgumentGuard.NotNull(resourceType);
3838
ArgumentGuard.NotNull(schemaRepository);
3939

40+
if (schemaRepository.TryLookupByType(dataContainerConstructedType, out OpenApiSchema referenceSchemaForData))
41+
{
42+
return referenceSchemaForData;
43+
}
44+
4045
if (!forRequestSchema)
4146
{
4247
// There's no way to intercept in the Swashbuckle recursive component schema generation when using schema inheritance, which we need
@@ -45,7 +50,7 @@ public OpenApiSchema GenerateSchema(Type dataContainerConstructedType, ResourceT
4550
_ = _abstractResourceDataSchemaGenerator.GenerateSchema(schemaRepository);
4651
}
4752

48-
Type dataConstructedType = GetInnerTypeOfDataProperty(dataContainerConstructedType, resourceType);
53+
Type dataConstructedType = GetElementTypeOfDataProperty(dataContainerConstructedType, resourceType);
4954

5055
if (!forRequestSchema)
5156
{
@@ -54,7 +59,7 @@ public OpenApiSchema GenerateSchema(Type dataContainerConstructedType, ResourceT
5459
EnsureResourceDataInResponseDerivedTypesAreMappedInDiscriminator(dataConstructedType, schemaRepository);
5560
}
5661

57-
OpenApiSchema referenceSchemaForData = _dataSchemaGenerator.GenerateSchema(dataConstructedType, schemaRepository);
62+
referenceSchemaForData = _dataSchemaGenerator.GenerateSchema(dataConstructedType, schemaRepository);
5863

5964
if (!forRequestSchema)
6065
{
@@ -64,7 +69,7 @@ public OpenApiSchema GenerateSchema(Type dataContainerConstructedType, ResourceT
6469
return referenceSchemaForData;
6570
}
6671

67-
private static Type GetInnerTypeOfDataProperty(Type dataContainerConstructedType, ResourceType resourceType)
72+
private static Type GetElementTypeOfDataProperty(Type dataContainerConstructedType, ResourceType resourceType)
6873
{
6974
PropertyInfo? dataProperty = dataContainerConstructedType.GetProperty("Data");
7075

@@ -98,9 +103,9 @@ private void EnsureResourceDataInResponseDerivedTypesAreMappedInDiscriminator(Ty
98103
{
99104
var resourceTypeInfo = ResourceTypeInfo.Create(dataConstructedType, _resourceGraph);
100105

101-
foreach (ResourceType resourceType in _includeDependencyScanner.GetReachableRelatedTypes(resourceTypeInfo.ResourceType))
106+
foreach (ResourceType relatedType in _includeDependencyScanner.GetReachableRelatedTypes(resourceTypeInfo.ResourceType))
102107
{
103-
MapResourceDataInResponseDerivedTypeInDiscriminator(resourceType, schemaRepository);
108+
MapResourceDataInResponseDerivedTypeInDiscriminator(relatedType, schemaRepository);
104109
}
105110
}
106111
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/UnusedComponentSchemaCleaner.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal sealed class UnusedComponentSchemaCleaner : IDocumentFilter
1414
{
1515
public void Apply(OpenApiDocument document, DocumentFilterContext context)
1616
{
17+
ArgumentGuard.NotNull(document);
18+
ArgumentGuard.NotNull(context);
19+
1720
bool hasChanges;
1821

1922
do
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Runtime.CompilerServices;
22

3-
[assembly: InternalsVisibleTo("JsonApiDotNetCore.OpenApi.Swashbuckle")]
43
[assembly: InternalsVisibleTo("Benchmarks")]
54
[assembly: InternalsVisibleTo("DapperExample")]
5+
[assembly: InternalsVisibleTo("JsonApiDotNetCore.OpenApi.Swashbuckle")]
66
[assembly: InternalsVisibleTo("JsonApiDotNetCoreTests")]
77
[assembly: InternalsVisibleTo("UnitTests")]

test/OpenApiKiotaEndToEndTests/OpenApiKiotaEndToEndTests.csproj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
2626
</ItemGroup>
2727

28-
<Target Name="KiotaRunTool" BeforeTargets="BeforeCompile;CoreCompile" Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
28+
<Target Name="ExcludeKiotaGeneratedCode" BeforeTargets="BeforeCompile;CoreCompile" Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
29+
<ItemGroup>
30+
<Compile Remove="**\GeneratedCode\**\*.cs" />
31+
</ItemGroup>
32+
</Target>
33+
34+
<Target Name="KiotaRunTool" BeforeTargets="BeforeCompile;CoreCompile" AfterTargets="ExcludeKiotaGeneratedCode"
35+
Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
2936
<Exec
3037
Command="dotnet kiota generate --language CSharp --class-name HeadersClient --namespace-name OpenApiKiotaEndToEndTests.Headers.GeneratedCode --output ./Headers/GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --log-level Error --openapi ../OpenApiTests/Headers/GeneratedSwagger/swagger.g.json" />
3138
<Exec
@@ -40,10 +47,11 @@
4047
Command="dotnet kiota generate --language CSharp --class-name LinksClient --namespace-name OpenApiKiotaEndToEndTests.Links.GeneratedCode --output ./Links/GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --log-level Error --openapi ../OpenApiTests/Links/Enabled/GeneratedSwagger/swagger.g.json" />
4148
<Exec
4249
Command="dotnet kiota generate --language CSharp --class-name AtomicOperationsClient --namespace-name OpenApiKiotaEndToEndTests.AtomicOperations.GeneratedCode --output ./AtomicOperations/GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --log-level Error --openapi ../OpenApiTests/AtomicOperations/GeneratedSwagger/swagger.g.json" />
50+
</Target>
4351

52+
<Target Name="IncludeKiotaGeneratedCode" BeforeTargets="BeforeCompile;CoreCompile" AfterTargets="KiotaRunTool"
53+
Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
4454
<ItemGroup>
45-
<!-- This isn't entirely reliable: may require a second build after the source swagger.json has changed, to get rid of compile errors. -->
46-
<Compile Remove="**\GeneratedCode\**\*.cs" />
4755
<Compile Include="**\GeneratedCode\**\*.cs" />
4856
</ItemGroup>
4957
</Target>

test/OpenApiKiotaEndToEndTests/QueryStrings/IncludeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
186186

187187
response.Included.ShouldHaveCount(1);
188188

189-
NodeDataInResponse? include = response.Included.ElementAt(0).Should().BeOfType<NodeDataInResponse>().Subject;
189+
NodeDataInResponse include = response.Included.ElementAt(0).Should().BeOfType<NodeDataInResponse>().Subject;
190190
include.Id.Should().Be(node.Parent.Parent.StringId);
191191
include.Attributes.ShouldNotBeNull();
192192
include.Attributes.Name.Should().Be(node.Parent.Parent.Name);

test/OpenApiKiotaEndToEndTests/RestrictedControllers/CreateResourceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
118118
audioStream.Attributes.ShouldNotBeNull();
119119
audioStream.Attributes.BytesTransmitted.Should().Be((long?)existingAudioStream.BytesTransmitted);
120120

121-
long newChannelId = int.Parse(response.Data.Id.ShouldNotBeNull());
121+
long newChannelId = long.Parse(response.Data.Id.ShouldNotBeNull());
122122

123123
await _testContext.RunOnDatabaseAsync(async dbContext =>
124124
{

test/OpenApiNSwagClientTests/BaseOpenApiNSwagClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public abstract class BaseOpenApiNSwagClientTests
1515

1616
ParameterExpression parameter = Expression.Parameter(attributesObjectType, AttributesObjectParameterName);
1717
MemberExpression property = Expression.Property(parameter, propertyName);
18-
UnaryExpression toObjectConversion = Expression.Convert(property, typeof(object));
18+
UnaryExpression castToObject = Expression.Convert(property, typeof(object));
1919

20-
return Expression.Lambda<Func<TAttributesObject, object?>>(toObjectConversion, parameter);
20+
return Expression.Lambda<Func<TAttributesObject, object?>>(castToObject, parameter);
2121
}
2222

2323
/// <summary>

0 commit comments

Comments
 (0)