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

Allow setting of the client interface access modifier based on the model access modifier #4820

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions src/NSwag.CodeGeneration.CSharp.Tests/CSharpClientSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ public async Task When_client_base_interface_is_not_specified_then_client_interf
Assert.Contains("public partial interface IFooClient\n", code);
}

[Fact]
public async Task When_client_base_interface_is_not_specified_then_client_interface_should_have_no_base_interface_and_has_correct_access_modifier()
{
// Arrange
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings()
});

var document = await swaggerGenerator.GenerateForControllerAsync<FooController>();
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
{
GenerateClientInterfaces = true,
ClientInterfaceAccessModifier = "internal"
});

// Act
var code = generator.GenerateFile();

// Assert
Assert.Contains("internal partial interface IFooClient\n", code);
}

[Fact]
public async Task When_client_base_interface_is_specified_then_client_interface_extends_it()
{
Expand All @@ -198,6 +221,30 @@ public async Task When_client_base_interface_is_specified_then_client_interface_
Assert.Contains("public partial interface IFooClient : IClientBase", code);
}

[Fact]
public async Task When_client_base_interface_is_specified_with_access_modifier_then_client_interface_extends_it_and_has_correct_access_modifier()
{
// Arrange
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings()
});

var document = await swaggerGenerator.GenerateForControllerAsync<FooController>();
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
{
GenerateClientInterfaces = true,
ClientBaseInterface = "IClientBase",
ClientInterfaceAccessModifier = "internal"
});

// Act
var code = generator.GenerateFile();

// Assert
Assert.Contains("internal partial interface IFooClient : IClientBase", code);
}

[Fact]
public async Task When_client_class_generation_is_enabled_and_suppressed_then_client_class_is_not_generated()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public CSharpClientGeneratorSettings()
GenerateExceptionClasses = true;
ExceptionClass = "ApiException";
ClientClassAccessModifier = "public";
ClientInterfaceAccessModifier = "public";
UseBaseUrl = true;
HttpClientType = "System.Net.Http.HttpClient";
WrapDtoExceptions = true;
Expand Down Expand Up @@ -70,6 +71,9 @@ public CSharpClientGeneratorSettings()
/// <summary>Gets or sets the client class access modifier (default: public).</summary>
public string ClientClassAccessModifier { get; set; }

/// <summary>Gets or sets the client interface access modifier (default: public).</summary>
public string ClientInterfaceAccessModifier { get; set; }

/// <summary>Gets or sets a value indicating whether to use and expose the base URL (default: true).</summary>
public bool UseBaseUrl { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public CSharpClientTemplateModel(
/// <summary>Gets or sets the client class access modifier.</summary>
public string ClientClassAccessModifier => _settings.ClientClassAccessModifier;

/// <summary>Gets or sets the client interface access modifier.</summary>
public string ClientInterfaceAccessModifier => _settings.ClientInterfaceAccessModifier;

/// <summary>Gets the operations.</summary>
public IEnumerable<CSharpOperationModel> Operations { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% template Client.Interface.Annotations %}
[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
public partial interface I{{ Class }}{% if HasClientBaseInterface %} : {{ ClientBaseInterface }}{% endif %}
{{ ClientInterfaceAccessModifier }} partial interface I{{ Class }}{% if HasClientBaseInterface %} : {{ ClientBaseInterface }}{% endif %}
{
{% template Client.Interface.Body %}
{% for operation in InterfaceOperations -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,16 @@ public string ClientClassAccessModifier
set { Settings.ClientClassAccessModifier = value; }
}


[Argument(Name = "TypeAccessModifier", IsRequired = false, Description = "The DTO class/enum access modifier (default: public).")]
public string TypeAccessModifier
{
get { return Settings.CSharpGeneratorSettings.TypeAccessModifier; }
set { Settings.CSharpGeneratorSettings.TypeAccessModifier = value; }
set
{
Settings.CSharpGeneratorSettings.TypeAccessModifier = value;
Settings.ClientInterfaceAccessModifier = value;
}
}

[Argument(Name = "PropertySetterAccessModifier", IsRequired = false, Description = "The access modifier of property setters (default: '').")]
Expand Down
Loading