diff --git a/src/Refitter.Core/SchemaCleaner.cs b/src/Refitter.Core/SchemaCleaner.cs index a5c872c8..fd47320f 100644 --- a/src/Refitter.Core/SchemaCleaner.cs +++ b/src/Refitter.Core/SchemaCleaner.cs @@ -54,7 +54,7 @@ HashSet FindUsedSchema(OpenApiDocument doc) continue; } } - foreach (var subSchema in EnumerateSchema(schema)) + foreach (var subSchema in EnumerateSchema(schema.ActualSchema)) { TryPush(subSchema, toProcess); } diff --git a/src/Refitter.Tests/Examples/TrimUnusedSchemaTests.cs b/src/Refitter.Tests/Examples/TrimUnusedSchemaTests.cs index 03f75b29..7ec258bc 100644 --- a/src/Refitter.Tests/Examples/TrimUnusedSchemaTests.cs +++ b/src/Refitter.Tests/Examples/TrimUnusedSchemaTests.cs @@ -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] @@ -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 PerformLogin([Body] LoginRequest "); - - generateCode.Should().NotContain("class User"); - generateCode.Should().NotContain("class RefreshLoginResult"); - generateCode.Should().NotContain("class RefreshLoginRequest"); + generateCode.Should().Contain("Task CreateWarehouse([Body] Warehouse "); + + generateCode.Should().NotContain("class UserComponent"); } [Fact]