Skip to content

Commit

Permalink
Update templates
Browse files Browse the repository at this point in the history
  • Loading branch information
justinyoo committed Jul 19, 2020
1 parent c3decc3 commit b6b563f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions templates/OpenApiEndpints/OpenApiHttpTriggerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public virtual OpenApiSpecVersion GetOpenApiSpecVersion(OpenApiVersionType versi
/// <inheritdoc />
public virtual OpenApiFormat GetOpenApiFormat(string format = "json")
{
if (format.Equals("yml", StringComparison.InvariantCultureIgnoreCase))
{
format = "yaml";
}

var parsed = Enum.TryParse(format, true, out OpenApiFormatType output)
? output
: throw new InvalidOperationException("Invalid Open API format");
Expand Down
84 changes: 84 additions & 0 deletions templates/OpenApiEndpints/OpenApiHttpTriggerV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ public static async Task<HttpResponseMessage> RenderSwaggerDocumentInJson(
return response;
}

/// <summary>
/// Invokes the HTTP trigger endpoint to get Swagger document in a format of YAML.
/// </summary>
/// <param name="req"><see cref="HttpRequestMessage"/> instance.</param>
/// <param name="log"><see cref="ILogger"/> instance.</param>
/// <returns>Swagger document in a format in YAML.</returns>
[FunctionName(nameof(OpenApiHttpTrigger.RenderSwaggerDocumentInYaml))]
[OpenApiIgnore]
public static async Task<HttpResponseMessage> RenderSwaggerDocumentInYaml(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "swagger.yml")] HttpRequestMessage req,
ILogger log)
{
log.LogInformation($"swagger.yaml was requested.");

var result = await context.Document
.InitialiseDocument()
.AddMetadata(context.OpenApiInfo)
.AddServer(req, context.HttpSettings.RoutePrefix)
.Build(context.GetExecutingAssembly())
.RenderAsync(context.GetOpenApiSpecVersion(V2), context.GetOpenApiFormat(YAML))
.ConfigureAwait(false);

var content = new StringContent(result, Encoding.UTF8, context.GetOpenApiFormat(YAML).GetContentType());
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = content };

return response;
}

/// <summary>
/// Invokes the HTTP trigger endpoint to get Swagger document in a format of YAML.
/// </summary>
Expand Down Expand Up @@ -109,6 +137,34 @@ public static async Task<HttpResponseMessage> RenderOpenApiDocumentV2InJson(
return response;
}

/// <summary>
/// Invokes the HTTP trigger endpoint to get Open API document v2 in a format of YAML.
/// </summary>
/// <param name="req"><see cref="HttpRequestMessage"/> instance.</param>
/// <param name="log"><see cref="ILogger"/> instance.</param>
/// <returns>Open API document v2 in a format of YAML.</returns>
[FunctionName(nameof(OpenApiHttpTrigger.RenderOpenApiDocumentV2InYaml))]
[OpenApiIgnore]
public static async Task<HttpResponseMessage> RenderOpenApiDocumentV2InYaml(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "openapi/v2.yml")] HttpRequestMessage req,
ILogger log)
{
log.LogInformation($"v2.yaml was requested.");

var result = await context.Document
.InitialiseDocument()
.AddMetadata(context.OpenApiInfo)
.AddServer(req, context.HttpSettings.RoutePrefix)
.Build(context.GetExecutingAssembly())
.RenderAsync(context.GetOpenApiSpecVersion(V2), context.GetOpenApiFormat(YAML))
.ConfigureAwait(false);

var content = new StringContent(result, Encoding.UTF8, context.GetOpenApiFormat(YAML).GetContentType());
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = content };

return response;
}

/// <summary>
/// Invokes the HTTP trigger endpoint to get Open API document v2 in a format of YAML.
/// </summary>
Expand Down Expand Up @@ -165,6 +221,34 @@ public static async Task<HttpResponseMessage> RenderOpenApiDocumentV3InJson(
return response;
}

/// <summary>
/// Invokes the HTTP trigger endpoint to get Open API document v3 in a format of YAML.
/// </summary>
/// <param name="req"><see cref="HttpRequestMessage"/> instance.</param>
/// <param name="log"><see cref="ILogger"/> instance.</param>
/// <returns>Open API document v3 in a format of YAML.</returns>
[FunctionName(nameof(OpenApiHttpTrigger.RenderOpenApiDocumentV3InYaml))]
[OpenApiIgnore]
public static async Task<HttpResponseMessage> RenderOpenApiDocumentV3InYaml(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "openapi/v3.yml")] HttpRequestMessage req,
ILogger log)
{
log.LogInformation($"v3.yaml was requested.");

var result = await context.Document
.InitialiseDocument()
.AddMetadata(context.OpenApiInfo)
.AddServer(req, context.HttpSettings.RoutePrefix)
.Build(context.GetExecutingAssembly())
.RenderAsync(context.GetOpenApiSpecVersion(V3), context.GetOpenApiFormat(YAML))
.ConfigureAwait(false);

var content = new StringContent(result, Encoding.UTF8, context.GetOpenApiFormat(YAML).GetContentType());
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = content };

return response;
}

/// <summary>
/// Invokes the HTTP trigger endpoint to get Open API document v3 in a format of YAML.
/// </summary>
Expand Down

0 comments on commit b6b563f

Please sign in to comment.