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 @@ -95,6 +95,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
private LinkedHashMap<String, CodegenMediaType> content;
private Map<String, CodegenProperty> requiredVarsMap;
private String ref;
public CodegenProperty returnProperty;
private boolean schemaIsFromAdditionalProperties;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
.put("backslash", new BackSlashLambda())
.put("doublequote", new DoubleQuoteLambda())
.put("indented", new IndentedLambda())
.put("indented_8", new IndentedLambda(8, " ", false))
.put("indented_12", new IndentedLambda(12, " ", false))
.put("indented_16", new IndentedLambda(16, " ", false));
.put("indented_8", new IndentedLambda(8, " ", false, false))
.put("indented_12", new IndentedLambda(12, " ", false, false))
.put("indented_16", new IndentedLambda(16, " ", false, false));

}

Expand Down Expand Up @@ -4850,6 +4850,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {

CodegenProperty cp = fromProperty("response", responseSchema, false);
r.dataType = getTypeDeclaration(responseSchema);
r.returnProperty = cp;

if (!ModelUtils.isArraySchema(responseSchema)) {
if (cp.complexType != null) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void processOpts() {
@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
.put("multiline_comment_4", new IndentedLambda(4, " ", "///", false));
.put("multiline_comment_4", new IndentedLambda(4, " ", "///", false, false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void processOpts() {
@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
.put("indented_4", new IndentedLambda(4, " ", false));
.put("indented_4", new IndentedLambda(4, " ", false, false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ protected String getEnumDefaultValue(String defaultValue, String dataType) {
@Override
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
ImmutableMap.Builder<String, Mustache.Lambda> lambdas = super.addMustacheLambdas();
lambdas.put("indented_star_1", new IndentedLambda(1, " ", "* ", false));
lambdas.put("indented_star_4", new IndentedLambda(5, " ", "* ", false));
lambdas.put("indented_star_1", new IndentedLambda(1, " ", "* ", false, false));
lambdas.put("indented_star_4", new IndentedLambda(5, " ", "* ", false, false));
return lambdas;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public class IndentedLambda implements Mustache.Lambda {
private final String prefix;
private final int spaceCode;
private final boolean indentFirstLine;
private final boolean skipEmptyLines;

/**
* Constructs a new instance of {@link IndentedLambda}, with an indent count of 4 spaces
*/
public IndentedLambda() {
this(4, " ", null, false);
this(4, " ", null, false, false);
}

/**
Expand All @@ -61,8 +62,8 @@ public IndentedLambda() {
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
*/
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, boolean indentFirstLine) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), null, indentFirstLine);
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, boolean indentFirstLine, boolean skipEmptyLines) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), null, indentFirstLine, skipEmptyLines);
}

/**
Expand All @@ -73,8 +74,8 @@ public IndentedLambda(int prefixSpaceCount, String indentionCharacter, boolean i
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
*/
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix, boolean indentFirstLine) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix, indentFirstLine);
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix, boolean indentFirstLine, boolean skipEmptyLines) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix, indentFirstLine, skipEmptyLines);
}

/**
Expand All @@ -85,7 +86,7 @@ public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String pr
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
*/
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix, boolean indentFirstLine) {
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix, boolean indentFirstLine, boolean skipEmptyLines) {
if (prefixSpaceCount <= 0) {
throw new IllegalArgumentException("prefixSpaceCount must be greater than 0");
}
Expand All @@ -98,6 +99,7 @@ private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String pref
this.spaceCode = indentionCodePoint;
this.prefix = prefix;
this.indentFirstLine = indentFirstLine;
this.skipEmptyLines = skipEmptyLines;
}

@Override
Expand All @@ -116,7 +118,9 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
// Mustache will apply correct indentation to the first line of a template (to match declaration location).
// So, we want to skip the first line.
if (this.indentFirstLine || i > 0) {
sb.append(prefixedIndention);
if (!this.skipEmptyLines || line.trim().length() > 0) {
sb.append(prefixedIndention);
}
if (prefix != null) sb.append(prefix);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace {{packageName}}.{{clientPackage}}
/// <summary>
/// Useful for tracking server health
/// </summary>
{{>visibility}} class ApiResponseEventArgs<T> : EventArgs
{{>visibility}} class ApiResponseEventArgs : EventArgs
{
/// <summary>
/// The ApiResponse
/// </summary>
public ApiResponse<T> ApiResponse { get; }
public ApiResponse ApiResponse { get; }

/// <summary>
/// The ApiResponseEventArgs
/// </summary>
/// <param name="apiResponse"></param>
public ApiResponseEventArgs(ApiResponse<T> apiResponse)
public ApiResponseEventArgs(ApiResponse apiResponse)
{
ApiResponse = apiResponse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@

{{/nrt}}
using System;
using System.Collections.Generic;
{{^netStandard}}
using System.Diagnostics.CodeAnalysis;
{{/netStandard}}
using System.Net;

namespace {{packageName}}.{{clientPackage}}
{
/// <summary>
/// Provides a non-generic contract for the ApiResponse wrapper.
/// </summary>
{{>visibility}} interface IApiResponse
{{>visibility}} partial interface IApiResponse
{
/// <summary>
/// The type that represents the server's response.
/// The IsSuccessStatusCode from the api response
/// </summary>
Type ResponseType { get; }
bool IsSuccessStatusCode { get; }

/// <summary>
/// Gets or sets the status code (HTTP status code)
/// Gets the status code (HTTP status code)
/// </summary>
/// <value>The status code.</value>
HttpStatusCode StatusCode { get; }
Expand All @@ -37,11 +38,26 @@ namespace {{packageName}}.{{clientPackage}}
/// </summary>
DateTime DownloadedAt { get; }

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

/// <summary>
/// The path used when making the request.
/// </summary>
string Path { get; }

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

/// <summary>
/// The DateTime when the request was sent.
/// </summary>
DateTime RequestedAt { get; }

/// <summary>
/// The Uri used when making the request.
/// </summary>
Expand All @@ -51,26 +67,18 @@ namespace {{packageName}}.{{clientPackage}}
/// <summary>
/// API Response
/// </summary>
{{>visibility}} partial class ApiResponse<T> : IApiResponse
{{>visibility}} partial class ApiResponse : IApiResponse
{
/// <summary>
/// Gets or sets the status code (HTTP status code)
/// Gets the status code (HTTP status code)
/// </summary>
/// <value>The status code.</value>
public HttpStatusCode StatusCode { get; }

/// <summary>
/// The type that represents the server's response.
/// </summary>
public Type ResponseType
{
get { return typeof(T); }
}

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

/// <summary>
/// The IsSuccessStatusCode from the api response
Expand Down Expand Up @@ -108,9 +116,9 @@ namespace {{packageName}}.{{clientPackage}}
public Uri{{nrt?}} RequestUri { get; }

/// <summary>
/// The JsonSerialzierOptions
/// The <see cref="System.Text.Json.JsonSerializerOptions"/>
/// </summary>
private System.Text.Json.JsonSerializerOptions _jsonSerializerOptions;
protected System.Text.Json.JsonSerializerOptions _jsonSerializerOptions;

/// <summary>
/// Construct the response using an HttpResponseMessage
Expand All @@ -136,32 +144,27 @@ namespace {{packageName}}.{{clientPackage}}
}

partial void OnCreated(System.Net.Http.HttpRequestMessage httpRequestMessage, System.Net.Http.HttpResponseMessage httpResponseMessage);
}
{{#x-http-statuses-with-return}}

/// <summary>
/// An interface for responses of type {{TType}}
/// </summary>
/// <typeparam name="TType"></typeparam>
{{>visibility}} interface I{{.}}<TType> : IApiResponse
{
/// <summary>
/// Deserializes the server's response
/// Deserializes the response if the response is {{.}}
/// </summary>
public T{{nrt?}} AsModel()
{
{{#lambda.trimTrailingWithNewLine}}
{{>AsModel}}
{{/lambda.trimTrailingWithNewLine}}
}
/// <returns></returns>
TType {{.}}();

/// <summary>
/// Returns true when the model can be deserialized
/// Returns true if the response is {{.}} and the deserialized response is not null
/// </summary>
public bool TryToModel({{^netStandard}}[NotNullWhen(true)] {{/netStandard}}out T{{nrt?}} model)
{
try
{
model = AsModel();
return model != null;
}
catch
{
model = default(T);
return false;
}
}
/// <param name="result"></param>
/// <returns></returns>
bool Try{{.}}({{^netStandard}}[NotNullWhen(true)]{{/netStandard}}out TType{{nrt?}} result);
}
{{/x-http-statuses-with-return}}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This logic may be modified with the AsModel.mustache template
return IsSuccessStatusCode
? System.Text.Json.JsonSerializer.Deserialize<T>(RawContent, _jsonSerializerOptions)
: default(T);
// This logic may be modified with the AsModel.mustache template
return Is{{vendorExtensions.x-http-status}}
? System.Text.Json.JsonSerializer.Deserialize<{{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}>(RawContent, _jsonSerializerOptions)
: {{^netStandard}}null{{/netStandard}}{{#netStandard}}default{{/netStandard}};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (!suppressDefaultLog)
Logger.LogError(exception, "An error occurred while deserializing the {code} response.", httpStatusCode);
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ namespace YourProject
{{#-first}}
{{#operation}}
{{#-first}}
ApiResponse<{{#returnType}}{{{.}}}{{/returnType}}{{^returnType}}object{{/returnType}}> response = await api.{{operationId}}Async("todo");
{{#returnType}}{{{.}}}{{/returnType}}{{^returnType}}object{{/returnType}} model = response.AsModel();
{{operationId}}ApiResponse apiResponse = await api.{{operationId}}Async("todo");
{{#returnType}}{{{.}}}{{/returnType}}{{^returnType}}object{{/returnType}} model = apiResponse.Ok();
{{/-first}}
{{/operation}}
{{/-first}}
Expand Down
Loading