Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/Refitter.Core/RefitInterfaceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,32 @@ protected string GetTypeName(OpenApiOperation operation)
return type is null or "void" ? GetAsyncOperationType(true) : $"{GetAsyncOperationType(false)}<{WellKnownNamesspaces.TrimImportedNamespaces(type)}>";
}

var returnTypeParameter =
(new[] { "200", "201", "203", "206" })
.Where(operation.Responses.ContainsKey)
.Select(code => GetTypeName(code, operation))
.FirstOrDefault();
// First check for explicit success status codes
var successCodes = new[] { "200", "201", "203", "206" };
var returnTypeParameter = successCodes
.Where(operation.Responses.ContainsKey)
.Select(code => GetTypeName(code, operation))
.FirstOrDefault();

// If no explicit success codes found, check for range responses in precedence order
string[] ranges = { "1XX", "2XX", "3XX", "4XX", "5XX" };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already merged, still as a feedback: Does it really make sense to allow the non-positive response wildcards?

What I mean is: Shouldn't this be limited to 2XX. Because otherwise you should also allow things like 404, 500 and so on, which doesn't really make sense, right?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfeigl No it doesn't and they will also be removed. This was purely implemented by CoPilot in Agent mode as an experiment. This vibe coding is not really my thing and now I have to clean up

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive as it that an AI agent literally did everything and all I did was click on a button on GitHub to let an agent do all the work. The result had flaws in the end and human intervention is simply always required

if (returnTypeParameter == null)
{
foreach (var rangeCode in ranges)
{
if (operation.Responses.ContainsKey(rangeCode))
{
returnTypeParameter = GetTypeName(rangeCode, operation);
break;
}
}
}

// If no success codes or ranges found, check for default response
if (returnTypeParameter == null && operation.Responses.ContainsKey("default"))
{
returnTypeParameter = GetTypeName("default", operation);
}

return GetReturnType(returnTypeParameter);
}
Expand Down
189 changes: 189 additions & 0 deletions src/Refitter.Tests/Examples/DefaultResponseObjectTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using FluentAssertions;
using Refitter.Core;
using Refitter.Tests.Build;
using Xunit;

namespace Refitter.Tests.Examples;

public class DefaultResponseObjectTests
{
private const string OpenApiSpec = @"
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'Default Response API'
description: 'An API that uses default responses and range responses'
servers:
- url: 'https://api.example.com/v1'
paths:
/users:
get:
tags:
- 'Users'
summary: 'Get users list using default response'
description: 'Returns a list of users using default response type'
operationId: 'getUsersWithDefault'
responses:
default:
description: 'List of users with unknown status code'
content:
application/json:
schema:
type: 'array'
items:
$ref: '#/components/schemas/User'
/users/{id}:
get:
tags:
- 'Users'
summary: 'Get user by ID using default response'
description: 'Returns a user by ID using default response type'
operationId: 'getUserByIdWithDefault'
parameters:
- name: 'id'
in: 'path'
description: 'User ID'
required: true
schema:
type: 'string'
responses:
default:
description: 'User data with unknown status code'
content:
application/json:
schema:
$ref: '#/components/schemas/User'
/categories:
get:
tags:
- 'Categories'
summary: 'Get categories using 2XX range response'
description: 'Returns a list of categories using 2XX range response'
operationId: 'getCategoriesWithRange'
responses:
2XX:
description: 'List of categories with 2XX status code'
content:
application/json:
schema:
type: 'array'
items:
$ref: '#/components/schemas/Category'
/categories/{id}:
get:
tags:
- 'Categories'
summary: 'Get category by ID using 2XX range response'
description: 'Returns a category by ID using 2XX range response'
operationId: 'getCategoryByIdWithRange'
parameters:
- name: 'id'
in: 'path'
description: 'Category ID'
required: true
schema:
type: 'string'
responses:
2XX:
description: 'Category data with 2XX status code'
content:
application/json:
schema:
$ref: '#/components/schemas/Category'
components:
schemas:
User:
type: 'object'
properties:
id:
type: 'string'
name:
type: 'string'
email:
type: 'string'
format: 'email'
Category:
type: 'object'
properties:
id:
type: 'string'
name:
type: 'string'
";

[Fact]
public async Task Can_Generate_Code()
{
string generatedCode = await GenerateCode();
generatedCode.Should().NotBeNullOrWhiteSpace();
}

[Fact]
public async Task Can_Build_Generated_Code()
{
string generatedCode = await GenerateCode();
BuildHelper
.BuildCSharp(generatedCode)
.Should()
.BeTrue();
}

[Fact]
public async Task Should_Generate_Default_Response_Return_Types()
{
string generatedCode = await GenerateCode();
generatedCode.Should().Contain("Task<User> GetUserByIdWithDefault(string id);");
generatedCode.Should().Contain("Task<ICollection<User>> GetUsersWithDefault();");
}

[Fact]
public async Task Should_Generate_Range_Response_Return_Types()
{
string generatedCode = await GenerateCode();
generatedCode.Should().Contain("Task<Category> GetCategoryByIdWithRange(string id);");
generatedCode.Should().Contain("Task<ICollection<Category>> GetCategoriesWithRange();");
}

[Fact]
public async Task Should_Generate_User_Contract()
{
string generatedCode = await GenerateCode();
generatedCode.Should().Contain("public partial class User");
generatedCode.Should().Contain("public string Id { get; set; }");
generatedCode.Should().Contain("public string Name { get; set; }");
generatedCode.Should().Contain("public string Email { get; set; }");
}

[Fact]
public async Task Should_Generate_Category_Contract()
{
string generatedCode = await GenerateCode();
generatedCode.Should().Contain("public partial class Category");
generatedCode.Should().Contain("public string Id { get; set; }");
generatedCode.Should().Contain("public string Name { get; set; }");
}

private static async Task<string> GenerateCode()
{
var swaggerFile = await CreateSwaggerFile(OpenApiSpec);
var settings = new RefitGeneratorSettings
{
OpenApiPath = swaggerFile,
UseCancellationTokens = false
};

var sut = await RefitGenerator.CreateAsync(settings);
var generatedCode = sut.Generate();
return generatedCode;
}

private static async Task<string> CreateSwaggerFile(string contents)
{
var filename = $"{Guid.NewGuid()}.yml";
var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(folder);
var swaggerFile = Path.Combine(folder, filename);
await File.WriteAllTextAsync(swaggerFile, contents);
return swaggerFile;
}
}