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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ApiCodeGenerator.OpenApi.Sdk" Version="3.0.0" />
<PackageReference Include="ApiCodeGenerator.OpenApi.Sdk" Version="3.0.1" />
<PackageReference Include="NSwag.Core.Yaml" Version="14.0.2" />
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="14.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
Expand Down
95 changes: 79 additions & 16 deletions ApiCodeGenerator.OpenApi.Refit.Tests/FunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace ApiCodeGenerator.OpenApi.Refit.Tests
{
public class FunctionalTests
{
private const string NS = "TestNS";

[Test]
public void GenerateClientInterface()
{
Expand All @@ -16,7 +18,7 @@ public void GenerateClientInterface()
{
GenerateClientInterfaces = true,
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var expected =
" public partial interface IClient\n" +
" {\n" +
Expand Down Expand Up @@ -48,7 +50,7 @@ public void GenerateClientInterface_Query()
{
GenerateClientInterfaces = true,
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var expected =
" public partial interface IClient\n" +
" {\n" +
Expand Down Expand Up @@ -95,7 +97,7 @@ public void GenerateClientInterface_HeaderWithAlias()
var settings = new RefitCodeGeneratorSettings
{
GenerateClientInterfaces = true,
CSharpGeneratorSettings = { Namespace = "TestNS" },
CSharpGeneratorSettings = { Namespace = NS },
};

var expected =
Expand All @@ -119,7 +121,7 @@ public void GenerateClientInterface_BaseInterface()
{
GenerateClientInterfaces = true,
ClientBaseInterface = "IBase, IBase2",
CSharpGeneratorSettings = { Namespace = "TestNS" },
CSharpGeneratorSettings = { Namespace = NS },
};
var expected =
" public partial interface IClient : IBase, IBase2\n" +
Expand Down Expand Up @@ -153,7 +155,7 @@ public void GenerateClientInterface_InterfaceAccesModifier()
GenerateClientInterfaces = true,
InterfaceAccessModifier = "internal",
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var expected =
" internal partial interface IClient\n" +
" {\n" +
Expand Down Expand Up @@ -187,7 +189,7 @@ public void GenerateClientInterface_AsyncSuffix_CancelationToken()
OperationAsyncSuffix = true,
GenerateOptionalParameters = true,
OperationCancelationToken = true,
CSharpGeneratorSettings = { Namespace = "TestNS" },
CSharpGeneratorSettings = { Namespace = NS },
};

var expected =
Expand Down Expand Up @@ -225,7 +227,7 @@ public void GenerateClientInterface_ApiResponseOperations()
WrapResponses = true,
WrapResponseMethods = new[] { "Client.GetTestOper" },
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var expectedIntefaceDeclaration =
" public partial interface IClient\n" +
" {\n" +
Expand Down Expand Up @@ -270,7 +272,7 @@ public void GenerateClientInterface_ApiResponseOperations_PathExtract()
WrapResponses = true,
WrapResponseMethods = new[] { "Client.GetTestOper" },
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var expectedIntefaceDeclaration =
" public partial interface IClient\n" +
" {\n" +
Expand Down Expand Up @@ -312,7 +314,7 @@ public void GenerateClientInterface_MultipleClientsFromPathSegments()
GenerateClientInterfaces = true,
OperationNameGenerator = new MultipleClientsFromPathSegmentsOperationNameGenerator(),
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var document = OpenApiDocument.FromFileAsync(GetSwaggerPath("testSchema.json")).Result;
var openApiPathItem = new OpenApiPathItem
{
Expand Down Expand Up @@ -368,7 +370,7 @@ public void GenerateClientInterface_PathExtract()
GenerateClientInterfaces = true,
PathExtractExpression = @"^testService\/(.*)$",
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var expected =
" public partial interface IClient\n" +
" {\n" +
Expand Down Expand Up @@ -401,7 +403,7 @@ public void GenerateClientInterface_PathExtract_NotMatched()
GenerateClientInterfaces = true,
PathExtractExpression = @"^notmatch\/(.*)$",
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
var expected =
" public partial interface IClient\n" +
" {\n" +
Expand Down Expand Up @@ -433,7 +435,7 @@ public void GenerateClientInterface_BringDefaultParamsToEnd()
{
GenerateClientInterfaces = true,
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
settings.GenerateOptionalParameters = true;
var expected =
" public partial interface IClient\n" +
Expand Down Expand Up @@ -462,7 +464,7 @@ public void GenerateClientInterface_NotBringDefaultParamsToEnd_IfOptParamDisable
{
GenerateClientInterfaces = true,
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
settings.GenerateOptionalParameters = false;
var expected =
" public partial interface IClient\n" +
Expand Down Expand Up @@ -492,7 +494,7 @@ public void ExcludeParam()
ExcludedParameterNames = [
"paramWithDef"
],
CSharpGeneratorSettings = { Namespace = "TestNS" },
CSharpGeneratorSettings = { Namespace = NS },
};

var expected =
Expand Down Expand Up @@ -522,7 +524,7 @@ public void GenerateClientInterface_AuthHeaderParameter()
GenerateClientInterfaces = true,
AuthorizationHeaderParameter = true,
};
settings.CSharpGeneratorSettings.Namespace = "TestNS";
settings.CSharpGeneratorSettings.Namespace = NS;
settings.GenerateOptionalParameters = false;
var expected =
" public partial interface IClient\n" +
Expand Down Expand Up @@ -552,7 +554,7 @@ public void GenerateClientInterface_BinaryResponse()
GenerateOptionalParameters = false,
CSharpGeneratorSettings =
{
Namespace = "TestNS",
Namespace = NS,
},
};

Expand All @@ -567,5 +569,66 @@ public void GenerateClientInterface_BinaryResponse()

RunTest(settings, expected, "streamResponse.yaml", " \n");
}

[Test]
public async Task GenerateClientInterface_DtoForResponses()
{
var yaml = """
openapi: 3.0.0
info:
title: inlined response schema
version: 1.0.0
paths:
/test:
get:
responses:
"200":
description: OK
"400":
description: Bad
content:
"application/json":
schema:
type: object
properties:
errorCode:
type: string
errorMessage:
type: string
additionalProperties: false
""";
var doc = await OpenApiYamlDocument.FromYamlAsync(yaml);
var settings = new RefitCodeGeneratorSettings
{
GenerateClientInterfaces = true,
CSharpGeneratorSettings =
{
Namespace = NS,
},
};

var expected =
" public partial interface IClient\n" +
" {\n" +
" /// <returns>OK</returns>\n" +
" /// <exception cref=\"Refit.ApiException\">A server side error occurred.</exception>\n" +
" [Get(\"/test\")]\n" +
" System.Threading.Tasks.Task Test();\n" +
"\n" +
" }\n";

var response = " " + GENERATED_CODE + "\n" +
" public partial class TestResponse400\n" +
" {\n" +
" [Newtonsoft.Json.JsonProperty(\"errorCode\", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]\n" +
" public string ErrorCode { get; set; }\n" +
"\n" +
" [Newtonsoft.Json.JsonProperty(\"errorMessage\", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]\n" +
" public string ErrorMessage { get; set; }\n" +
"\n" +
" }\n";

RunTest(settings, expected, doc, response);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="ApiCodeGenerator.OpenApi.Sdk/3.0.0">
<Project Sdk="ApiCodeGenerator.OpenApi.Sdk/3.0.1">

<PropertyGroup>
<Description>Generates code for the Refit library from an OpenApi document.</Description>
Expand Down
17 changes: 17 additions & 0 deletions ApiCodeGenerator.OpenApi.Refit/RefitCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal RefitCodeGenerator(OpenApiDocument openApiDocument, RefitCodeGeneratorS
/// <inheritdoc />
protected override IEnumerable<CodeArtifact> GenerateClientTypes(string controllerName, string controllerClassName, IEnumerable<CSharpOperationModel> operations)
{
GenerateClientInlineReponses(operations);
RefitClientTemplateModel model = CreateTemplateModel(controllerName, 'I' + controllerClassName, operations);
if (model.HasOperations && model.GenerateClientInterfaces)
{
Expand Down Expand Up @@ -141,5 +142,21 @@ void CleanupProperties(IEnumerable<KeyValuePair<string, JsonSchemaProperty>> act
}
}
}

private void GenerateClientInlineReponses(IEnumerable<CSharpOperationModel> operations)
{
foreach (var operation in operations)
{
foreach (var response in operation.Responses)
{
var schema = response.ActualResponseSchema;
if (schema is not null && !schema.HasReference)
{
var hint = $"{operation.ActualOperationName}Response{response.StatusCode}";
Resolver.Resolve(schema, schema.IsNullable(_settings.CodeGeneratorSettings.SchemaType), hint);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public RefitCodeGeneratorSettings()
CodeGeneratorSettings,
[
GetType().Assembly,
typeof(CSharpClientGeneratorSettings).Assembly,
typeof(NSwag.CodeGeneration.CSharp.CSharpClientGeneratorSettings).Assembly,
typeof(NJsonSchema.CodeGeneration.CSharp.CSharpGenerator).Assembly,
]);

Expand Down
Loading