Skip to content
Closed
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
3 changes: 2 additions & 1 deletion docs/generators/csharp-netcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|interfacePrefix|Prefix interfaces with a community standard or widely accepted prefix.| |I|
|library|HTTP library template (sub-template) to use|<dl><dt>**httpclient**</dt><dd>HttpClient (https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) (Experimental. May subject to breaking changes without further notice.)</dd><dt>**restsharp**</dt><dd>RestSharp (https://github.com/restsharp/RestSharp)</dd></dl>|restsharp|
|library|HTTP library template (sub-template) to use|<dl><dt>**httpclient**</dt><dd>HttpClient (https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) (Experimental. May subject to breaking changes without further notice.)</dd><dt>**restsharp**</dt><dd>RestSharp (https://github.com/restsharp/RestSharp)</dd><dt>**httpclient-experimental**</dt><dd>HttpClient (https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) (Experimental. May subject to breaking changes without further notice.)</dd></dl>|restsharp|
|licenseId|The identifier of the license| |null|
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |PascalCase|
|netCoreProjectFile|Use the new format (.NET Core) for .NET project files (.csproj).| |false|
|nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.| |false|
|nullableReferenceTypes|Set the nullable reference types property to true or false. Default is false.| |false|
|optionalAssemblyInfo|Generate AssemblyInfo.cs.| |true|
|optionalEmitDefaultValues|Set DataMember's EmitDefaultValue.| |false|
|optionalMethodArgument|C# Optional method argument, e.g. void square(int x=10) (.net 4.0+ only).| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ public class CodegenConstants {
public static final String DOTNET_FRAMEWORK = "targetFramework";
public static final String DOTNET_FRAMEWORK_DESC = "The target .NET framework version.";

public static final String NULLABLE_REFERENCE_TYPES = "nullableReferenceTypes";
public static final String NULLABLE_REFERENCE_TYPES_DESC = "Set the nullable reference types property to true or false. Default is false.";

public static final String TEMPLATING_ENGINE = "templatingEngine";
public static final String TEMPLATING_ENGINE_DESC = "The templating engine plugin to use: \"mustache\" (default) or \"handlebars\" (beta)";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
protected boolean useCollection = false;
protected boolean returnICollection = false;
protected boolean netCoreProjectFileFlag = false;
protected boolean nullReferenceTypesFlag = false;

protected String modelPropertyNaming = CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.PascalCase.name();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
// HTTP libraries
protected static final String RESTSHARP = "restsharp";
protected static final String HTTPCLIENT = "httpclient";
protected static final String HTTPCLIENT_EXPERIMENTAL = "httpclient-experimental";

// Project Variable, determined from target framework. Not intended to be user-settable.
protected static final String TARGET_FRAMEWORK_IDENTIFIER = "targetFrameworkIdentifier";
Expand Down Expand Up @@ -230,6 +231,10 @@ public CSharpNetCoreClientCodegen() {
cliOptions.add(modelPropertyNaming.defaultValue("PascalCase"));

// CLI Switches
addSwitch(CodegenConstants.NULLABLE_REFERENCE_TYPES,
CodegenConstants.NULLABLE_REFERENCE_TYPES_DESC,
this.nullReferenceTypesFlag);

addSwitch(CodegenConstants.HIDE_GENERATION_TIMESTAMP,
CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC,
this.hideGenerationTimestamp);
Expand Down Expand Up @@ -303,6 +308,9 @@ public CSharpNetCoreClientCodegen() {
supportedLibraries.put(HTTPCLIENT, "HttpClient (https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) "
+ "(Experimental. May subject to breaking changes without further notice.)");
supportedLibraries.put(RESTSHARP, "RestSharp (https://github.com/restsharp/RestSharp)");
supportedLibraries.put(HTTPCLIENT_EXPERIMENTAL, "HttpClient (https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) "
+ "(Experimental. May subject to breaking changes without further notice.)");


CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "HTTP library template (sub-template) to use");
libraryOption.setEnum(supportedLibraries);
Expand Down Expand Up @@ -555,9 +563,21 @@ public void processOpts() {
* if (additionalProperties.containsKey(prop)) convertPropertyToBooleanAndWriteBack(prop);
*/

if (additionalProperties.containsKey(CodegenConstants.NULLABLE_REFERENCE_TYPES)){
Object nullableReferenceTypesFlag = additionalProperties.get(CodegenConstants.NULLABLE_REFERENCE_TYPES);
if (nullableReferenceTypesFlag == null || nullableReferenceTypesFlag.toString().trim().length() == 0 || nullableReferenceTypesFlag.toString().equals("true")){
writePropertyBack(CodegenConstants.NULLABLE_REFERENCE_TYPES, true);
this.setNullableReferenceTypes(true);
this.nullableType.add("string");
}
else{
writePropertyBack(CodegenConstants.NULLABLE_REFERENCE_TYPES, false);
}
}

if (additionalProperties.containsKey(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT)) {
this.setDisallowAdditionalPropertiesIfNotPresent(Boolean.valueOf(additionalProperties
.get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString()));
.get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString()));
}

if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES)) {
Expand Down Expand Up @@ -586,6 +606,9 @@ public void processOpts() {
setLibrary(HTTPCLIENT);
additionalProperties.put("useHttpClient", true);
needsUriBuilder = true;
}else if (HTTPCLIENT_EXPERIMENTAL.equals((getLibrary()))){
setLibrary(HTTPCLIENT_EXPERIMENTAL);
additionalProperties.put("useHttpClientExperimental", true);
} else {
throw new RuntimeException("Invalid HTTP library " + getLibrary() + ". Only restsharp, httpclient are supported.");
}
Expand Down Expand Up @@ -725,6 +748,10 @@ public void processOpts() {
additionalProperties.put("modelDocPath", modelDocPath);
}

public void setNullableReferenceTypes(Boolean flag){
this.nullReferenceTypesFlag = flag;
}

public void setNetStandard(Boolean netStandard) {
this.netStandard = netStandard;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{>partial_header}}

using System;

namespace {{packageName}}.Client
{
/// <summary>
/// API Exception
/// </summary>
{{>visibility}} class ApiException : Exception
{
/// <summary>
/// The reason the api request failed
/// </summary>
public string{{#nullableReferenceTypes}}?{{/nullableReferenceTypes}} ReasonPhrase { get; }

/// <summary>
/// The HttpStatusCode
/// </summary>
public System.Net.HttpStatusCode StatusCode { get; }

/// <summary>
/// The raw data returned by the api
/// </summary>
public string RawContent { get; }

/// <summary>
/// Construct the ApiException from parts of the reponse
/// </summary>
/// <param name="reasonPhrase"></param>
/// <param name="statusCode"></param>
/// <param name="rawContent"></param>
public ApiException(string{{#nullableReferenceTypes}}?{{/nullableReferenceTypes}} reasonPhrase, System.Net.HttpStatusCode statusCode, string rawContent) : base(reasonPhrase ?? rawContent)
{
ReasonPhrase = reasonPhrase;

StatusCode = statusCode;

RawContent = rawContent;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{{>partial_header}}

using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;

namespace {{packageName}}.Client
{
/// <summary>
/// Provides a non-generic contract for the ApiResponse wrapper.
/// </summary>
public interface IApiResponse
{
/// <summary>
/// The data type of <see cref="Data"/>
/// </summary>
Type ResponseType { get; }

/// <summary>
/// Gets or sets the status code (HTTP status code)
/// </summary>
/// <value>The status code.</value>
HttpStatusCode StatusCode { get; }

/// <summary>
/// Gets or sets any cookies passed along on the response.
/// </summary>
List<Cookie> Cookies { get; set; }

/// <summary>
/// The raw content of this response
/// </summary>
string RawContent { get; }
}

/// <summary>
/// API Response
/// </summary>
{{>visibility}} partial class ApiResponse<T> : IApiResponse
{
#region Properties

/// <summary>
/// The deserialized content
/// </summary>
public T{{#nullableReferenceTypes}}?{{/nullableReferenceTypes}} Content { get; set; }

/// <summary>
/// Gets or sets the status code (HTTP status code)
/// </summary>
/// <value>The status code.</value>
public HttpStatusCode StatusCode { get; }

/// <summary>
/// Gets or sets any cookies passed along on the response.
/// </summary>
public List<Cookie> Cookies { get; set; }

/// <summary>
/// The content of this response
/// </summary>
public Type ResponseType
{
get { return typeof(T); }
}

/// <summary>
/// The raw data
/// </summary>
public string RawContent { get; }

/// <summary>
/// The IsSuccessStatusCode from the api response
/// </summary>
public bool IsSuccessStatusCode { get; }

/// <summary>
/// The reason phrase contained in the api response
/// </summary>
public string{{#nullableReferenceTypes}}?{{/nullableReferenceTypes}} ReasonPhrase { get; }

/// <summary>
/// The headers contained in the api response
/// </summary>
public System.Net.Http.Headers.HttpResponseHeaders Headers { get; }

#endregion Properties

/// <summary>
/// Construct the reponse using an HttpResponseMessage
/// </summary>
/// <param name="response"></param>
/// <param name="rawContent"></param>
public ApiResponse(System.Net.Http.HttpResponseMessage response, string rawContent)
{
StatusCode = response.StatusCode;
Headers = response.Headers;
IsSuccessStatusCode = response.IsSuccessStatusCode;
ReasonPhrase = response.ReasonPhrase;
RawContent = rawContent;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("{{packageTitle}}")]
[assembly: AssemblyDescription("{{packageDescription}}")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("{{packageCompany}}")]
[assembly: AssemblyProduct("{{packageProductName}}")]
[assembly: AssemblyCopyright("{{packageCopyright}}")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("{{packageVersion}}")]
[assembly: AssemblyFileVersion("{{packageVersion}}")]
{{^supportsAsync}}
[assembly: InternalsVisibleTo("NewtonSoft.Json")]
[assembly: InternalsVisibleTo("JsonSubTypes")]
{{/supportsAsync}}
Loading