Skip to content

Commit

Permalink
Add more elaborate test Spec, fix wrong Schema being processed
Browse files Browse the repository at this point in the history
  • Loading branch information
kirides committed Oct 23, 2023
1 parent 56d697e commit 75ad57e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/Refitter.Core/SchemaCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ HashSet<string> FindUsedSchema(OpenApiDocument doc)
continue;
}
}
foreach (var subSchema in EnumerateSchema(schema))
foreach (var subSchema in EnumerateSchema(schema.ActualSchema))
{
TryPush(subSchema, toProcess);
}
Expand Down
158 changes: 95 additions & 63 deletions src/Refitter.Tests/Examples/TrimUnusedSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,116 +12,149 @@ public class TrimUnusedSchemaTests
private const string OpenApiSpec = @"
openapi: 3.0.1
paths:
/v1/Logins/login:
/v1/Warehouses:
post:
tags:
- Logins
operationId: PerformLogin
- Warehouses
operationId: CreateWarehouse
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/LoginRequest'
$ref: '#/components/schemas/Warehouse'
responses:
'200':
description: Success
'201':
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/LoginResult'
'422':
description: Client Error
$ref: '#/components/schemas/Warehouse'
'400':
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetails'
'500':
description: Server Error
components:
schemas:
LoginRequest:
Metadata:
type: object
properties:
username:
createdAt:
type: string
nullable: true
password:
type: string
nullable: true
additionalProperties: false
LoginResult:
type: object
properties:
userId:
type: string
format: uuid
accessToken:
format: date-time
createdBy:
type: string
nullable: true
accessTokenExpiresAt:
lastModifiedAt:
type: string
format: date-time
refreshToken:
lastModifiedBy:
type: string
nullable: true
refreshTokenExpiresAt:
additionalProperties: false
SomeComponent:
required:
- $type
type: object
allOf:
- $ref: '#/components/schemas/Component'
properties:
$type:
type: string
format: date-time
typeId:
type: integer
format: int64
additionalProperties: false
ProblemDetails:
discriminator:
propertyName: $type
SomeComponentState:
enum:
- Active
- Inactive
- Blocked
- Deleted
type: string
SomeComponentType:
type: object
allOf:
- $ref: '#/components/schemas/Component'
properties:
type:
state:
$ref: '#/components/schemas/SomeComponentState'
isBaseRole:
type: boolean
name:
type: string
nullable: true
title:
numberingId:
type: string
nullable: true
status:
additionalProperties: false
Component:
type: object
properties:
id:
type: integer
format: int32
nullable: true
detail:
format: int64
metadata:
$ref: '#/components/schemas/Metadata'
additionalProperties: false
LoadingAddress:
type: object
allOf:
- $ref: '#/components/schemas/SomeComponent'
properties:
info:
type: string
nullable: true
instance:
additionalProperties: false
Warehouse:
type: object
allOf:
- $ref: '#/components/schemas/SomeComponent'
properties:
info:
type: string
nullable: true
additionalProperties: { }
RefreshLoginRequest:
additionalProperties: false
UserComponent:
type: object
allOf:
- $ref: '#/components/schemas/SomeComponent'
properties:
refreshToken:
info:
type: string
nullable: true
additionalProperties: false
RefreshLoginResult:
ProblemDetails:
required:
- $type
type: object
properties:
userId:
$type:
type: string
format: uuid
accessToken:
type:
type: string
nullable: true
accessTokenExpiresAt:
type: string
format: date-time
refreshToken:
title:
type: string
nullable: true
refreshTokenExpiresAt:
type: string
format: date-time
additionalProperties: false
User:
type: object
properties:
id:
status:
type: integer
format: int32
nullable: true
detail:
type: string
nullable: true
username:
instance:
type: string
nullable: true
additionalProperties: false
additionalProperties: { }
discriminator:
propertyName: $type
";

[Fact]
Expand All @@ -135,15 +168,14 @@ public async Task Can_Generate_Code()
public async Task Removes_Unreferenced_Schema()
{
string generateCode = await GenerateCode();
generateCode.Should().Contain("class LoginRequest");
generateCode.Should().Contain("class LoginResult");
generateCode.Should().Contain("class Warehouse");
generateCode.Should().Contain("class SomeComponent");
generateCode.Should().Contain("class Component");
generateCode.Should().Contain("class ProblemDetails");

generateCode.Should().Contain("Task<LoginResult> PerformLogin([Body] LoginRequest ");

generateCode.Should().NotContain("class User");
generateCode.Should().NotContain("class RefreshLoginResult");
generateCode.Should().NotContain("class RefreshLoginRequest");
generateCode.Should().Contain("Task<Warehouse> CreateWarehouse([Body] Warehouse ");

generateCode.Should().NotContain("class UserComponent");
}

[Fact]
Expand Down

0 comments on commit 75ad57e

Please sign in to comment.