Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#362 add facility to exclude namespaces from generated output file(s) #365

Merged
merged 1 commit into from
Apr 11, 2024
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
12 changes: 12 additions & 0 deletions docs/docfx_project/articles/refitter-file-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ The following is an example `.refitter` file
"Namespace1",
"Namespace2"
],
"excludeNamespaces": [ // Optional. Exclude namespaces that match the provided regular expressions
"^Namespace[.].*",
"^Namespace$"
],
"includeTags": [ // Optional. OpenAPI Tag to include when generating code
"Pet",
"Store",
Expand Down Expand Up @@ -119,6 +123,7 @@ The following is an example `.refitter` file
- `outputFolder` - a string describing a relative path to a desired output folder. Default is `./Generated`
- `outputFilename` - Output filename. Default is `Output.cs` when used from the CLI tool, otherwise its the .refitter filename. So `Petstore.refitter` becomes `Petstore.cs`.
- `additionalNamespaces` - A collection of additional namespaces to include in the generated file. A use case for this is when you want to reuse contracts from a different namespace than the generated code. Default is empty
- `excludeNamespaces` - A collection of regular expressions to exclude namespaces from the generated file. A use case for this is when your project has global usings where these namepsaces would be redundant. Default is empty
- `includeTags` - A collection of tags to use a filter for including endpoints that contain this tag.
- `includePathMatches` - A collection of regular expressions used to filter paths.
- `generateDeprecatedOperations` - a boolean indicating whether deprecated operations should be generated or skipped. Default is `true`
Expand Down Expand Up @@ -263,6 +268,13 @@ The following is an example `.refitter` file
},
"description": "Additional namespaces to include in the generated code."
},
"excludeNamespaces": {
"type": "array",
"items": {
"type": "string"
},
"description": "Exclude namespaces that match the provided regular expressions."
},
"includeTags": {
"type": "array",
"items": {
Expand Down
17 changes: 12 additions & 5 deletions docs/json-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,18 @@
"description": "The output filename for the generated code."
},
"additionalNamespaces": {
"type": "array",
"items": {
"type": "string"
},
"description": "Additional namespaces to include in the generated code."
"type": "array",
"items": {
"type": "string"
},
"description": "Additional namespaces to include in the generated code."
},
"excludeNamespaces": {
"type": "array",
"items": {
"type": "string"
},
"description": "Exclude namespaces that match the provided regular expressions."
},
"includeTags": {
"type": "array",
Expand Down
16 changes: 15 additions & 1 deletion src/Refitter.Core/RefitInterfaceImports.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;

namespace Refitter.Core;

internal static class RefitInterfaceImports
{
private static string[] defaultNamespases = new[]
private static readonly string[] defaultNamespases = new[]
{
"Refit",
"System.Collections.Generic",
Expand All @@ -26,6 +28,18 @@ public static string[] GetImportedNamespaces(RefitGeneratorSettings settings)
{
namespaces.Add("System.Threading.Tasks");
}

if (settings.ExcludeNamespaces.Length != 0)
{
var exclusionNamespacesRegexes = settings.ExcludeNamespaces
.Where(n => !string.IsNullOrWhiteSpace(n))
.Select(x => new Regex(x, RegexOptions.Compiled))
.ToList();

var excludedNamespaces = exclusionNamespacesRegexes.SelectMany(k => namespaces.Where(x => k.IsMatch(x)));
namespaces = namespaces.Except(excludedNamespaces).ToList();
}

return namespaces.ToArray();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Refitter.Core/Settings/RefitGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public class RefitGeneratorSettings
/// </summary>
public string[] AdditionalNamespaces { get; set; } = Array.Empty<string>();

/// <summary>
/// Exclude namespaces on generated types
/// </summary>
public string[] ExcludeNamespaces { get; set; } = Array.Empty<string>();

/// <summary>
/// Set to <c>true</c> to Generate a Refit interface for each endpoint
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public partial interface ISwaggerPetstore

//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// Generated using the NSwag toolchain v14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------

Expand All @@ -390,14 +390,15 @@ public partial interface ISwaggerPetstore
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"
#pragma warning disable 8625 // Disable "CS8625 Cannot convert null literal to non-nullable reference type"
#pragma warning disable CS8765 // Nullability of type of parameter doesn't match overridden member (possibly because of nullability attributes).

namespace Refitter.Tests.AdditionalFiles.NoFilename
{
using System = global::System;



[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Order
{

Expand Down Expand Up @@ -435,7 +436,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Customer
{

Expand All @@ -459,7 +460,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Address
{

Expand All @@ -486,7 +487,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Category
{

Expand All @@ -507,7 +508,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class User
{

Expand Down Expand Up @@ -550,7 +551,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Tag
{

Expand All @@ -571,7 +572,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Pet
{

Expand Down Expand Up @@ -611,7 +612,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ApiResponse
{

Expand All @@ -635,7 +636,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum Status
{

Expand All @@ -650,7 +651,7 @@ public enum Status

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum OrderStatus
{

Expand All @@ -665,7 +666,7 @@ public enum OrderStatus

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum PetStatus
{

Expand All @@ -680,7 +681,7 @@ public enum PetStatus

}

[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class FileParameter
{
public FileParameter(System.IO.Stream data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public partial interface ISwaggerPetstoreWithOptionalParameters

//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// Generated using the NSwag toolchain v14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------

Expand All @@ -391,14 +391,15 @@ public partial interface ISwaggerPetstoreWithOptionalParameters
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"
#pragma warning disable 8625 // Disable "CS8625 Cannot convert null literal to non-nullable reference type"
#pragma warning disable CS8765 // Nullability of type of parameter doesn't match overridden member (possibly because of nullability attributes).

namespace Refitter.Tests.AdditionalFiles.OptionalParameters
{
using System = global::System;



[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Order
{

Expand Down Expand Up @@ -436,7 +437,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Customer
{

Expand All @@ -460,7 +461,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Address
{

Expand All @@ -487,7 +488,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Category
{

Expand All @@ -508,7 +509,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class User
{

Expand Down Expand Up @@ -551,7 +552,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Tag
{

Expand All @@ -572,7 +573,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Pet
{

Expand Down Expand Up @@ -612,7 +613,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ApiResponse
{

Expand All @@ -636,7 +637,7 @@ public IDictionary<string, object> AdditionalProperties

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum Status
{

Expand All @@ -651,7 +652,7 @@ public enum Status

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum OrderStatus
{

Expand All @@ -666,7 +667,7 @@ public enum OrderStatus

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum PetStatus
{

Expand All @@ -681,7 +682,7 @@ public enum PetStatus

}

[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class FileParameter
{
public FileParameter(System.IO.Stream data)
Expand Down
Loading
Loading