Skip to content

Commit 0c0e7c0

Browse files
committed
Update README.md
1 parent a72bba5 commit 0c0e7c0

File tree

4 files changed

+103
-54
lines changed

4 files changed

+103
-54
lines changed

docs/openapi-cli.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ This generates Open API document through command-line without having to run the
1212
* In order to read JSON configuration on Azure Functions 1.x, these extensions have copied the source code of [Microsoft.Extensions.Configuration.Json](https://github.com/aspnet/Extensions/tree/master/src/Configuration/Config.Json) to make use of [Newtonsoft.Json 9.0.1](https://www.nuget.org/packages/Newtonsoft.Json/9.0.1) under the [MIT License](http://opensource.org/licenses/MIT).
1313

1414

15+
## Issues? ##
16+
17+
While using this CLI, if you find any issue, please raise a ticket on the [Issue](https://github.com/aliencube/AzureFunctions.Extensions/issues) page.
18+
19+
1520
## Getting Started ##
1621

1722
### Download CLI ###
@@ -60,12 +65,12 @@ For Linux/MacOS
6065

6166
Here are options:
6267

63-
* `--project|-p`: Project path. It can be a fully qualified project path including `.csproj` or project directory. Default is the current directory.
64-
* `--configuration|-c`: Configuration value. It can be either `Debug`, `Release` or something else. Default is `Debug`.
65-
* `--target|-t`: Target framework. It should be `net4x` for Azure Functions v1, `netcoreapp2.x` for Azure Functions v2, and `netcoreapp3.x` for Azure Functions v3. Default is `netcoreapp2.1`.
66-
* `--version|-v`: Open API spec version. It should be either `v2` or `v3`. Default is `v2`.
67-
* `--format|-f`: Open API document format. It should be either `json` or `yaml`. Default is `json`.
68-
* `--output|-o`: Output directory for the generated Open API document. It can be a fully qualified directory path or relative path from `<PROJECT_ROOT>/bin/<CONFIGURATION>/<TARGET_FRAMEWORK>`. Default is `output`.
68+
* `-p|--project`: Project path. It can be a fully qualified project path including `.csproj` or project directory. Default is the current directory.
69+
* `-c|--configuration`: Configuration value. It can be either `Debug`, `Release` or something else. Default is `Debug`.
70+
* `-t|--target`: Target framework. It should be `net4x` for Azure Functions v1, `netcoreapp2.x` for Azure Functions v2, and `netcoreapp3.x` for Azure Functions v3. Default is `netcoreapp2.1`.
71+
* `-v|--version`: Open API spec version. It should be either `v2` or `v3`. Default is `v2`.
72+
* `-f|--format`: Open API document format. It should be either `json` or `yaml`. Default is `json`.
73+
* `-o|--output`: Output directory for the generated Open API document. It can be a fully qualified directory path or relative path from `<PROJECT_ROOT>/bin/<CONFIGURATION>/<TARGET_FRAMEWORK>`. Default is `output`.
6974
* `--console`: Value indicating whether to display the generated document to console or not. Default is `false`.
7075

7176

docs/openapi.md

Lines changed: 87 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,76 +13,83 @@ This enables Azure Functions to render Open API document and Swagger UI. The mor
1313
* [Swagger UI](https://github.com/swagger-api/swagger-ui) version used for this library is [3.20.5](https://github.com/swagger-api/swagger-ui/releases/tag/v3.20.5) under the [Apache 2.0 license](https://opensource.org/licenses/Apache-2.0).
1414

1515

16+
## Issues? ##
17+
18+
While using this library, if you find any issue, please raise a ticket on the [Issue](https://github.com/aliencube/AzureFunctions.Extensions/issues) page.
19+
20+
1621
## Getting Started ##
1722

18-
### Install Open API Templates ###
23+
### Install NuGet Package ###
24+
25+
In order for your Azure Functions app to enable Open API capability, download the following NuGet package into your Azure Functions project.
26+
27+
```bash
28+
dotnet add <PROJECT> package Aliencube.AzureFunctions.Extensions.OpenApi
29+
```
30+
31+
32+
### Option 1. Install Open API Boilerplate Templates ###
1933

2034
This is the easiest way to integrate the Open API document rendering. Run the following script based on your preference, which will install the pre-defined Open API document rendering endpoints.
2135

36+
2237
#### Download Installation Scripts ###
2338

24-
First of all, download the installation script from GitHub.
39+
Download the installation script using the PowerShell command.
2540

2641
```powershell
2742
# PowerShell
2843
(Invoke-WebRequest `
2944
-Uri "https://raw.githubusercontent.com/aliencube/AzureFunctions.Extensions/dev/scripts/Install-OpenApiHttpTriggerTemplates.ps1" `
30-
-UseBasicParsing).Content | Out-File "scripts/Install-OpenApiHttpTriggerTemplates.ps1"
45+
-UseBasicParsing).Content | Out-File "<DIRECTORY>/Install-OpenApiHttpTriggerTemplates.ps1"
3146
```
3247

48+
Alternatively, download the installation script using the Bash shell command. Make sure that the downloaded script MUST become executable.
49+
3350
```bash
3451
# Bash
35-
curl -X GET "https://raw.githubusercontent.com/aliencube/AzureFunctions.Extensions/dev/scripts/Install-OpenApiHttpTriggerTemplates.sh" > "scripts/Install-OpenApiHttpTriggerTemplates.sh"
52+
curl -X GET "https://raw.githubusercontent.com/aliencube/AzureFunctions.Extensions/dev/scripts/Install-OpenApiHttpTriggerTemplates.sh" \
53+
> "<DIRECTORY>/Install-OpenApiHttpTriggerTemplates.sh"
54+
55+
chmod +x <DIRECTORY>/Install-OpenApiHttpTriggerTemplates.sh
3656
```
3757

38-
For bash script, run the following command to enable an appropriate permission.
58+
> **NOTE**: Bash command does not support Azure Functions V1.
3959
40-
```bash
41-
# Bash
42-
chmod +x scripts/Download-OpenApiHttpTriggerTemplates.sh
43-
```
4460

45-
#### Install Open API Templates ####
61+
#### Install Open API Boilerplate Templates ####
4662

47-
Run the following script to install the Open API templates.
63+
Run the following script to install the Open API boilerplate templates in PowerShell.
4864

4965
```powershell
5066
# PowerShell
51-
scripts/Install-OpenApiHttpTriggerTemplates.ps1 `
52-
-ProjectPath ./samples/Aliencube.AzureFunctions.FunctionAppV3Static `
53-
-Namespace Aliencube.AzureFunctions.FunctionAppV3Static
67+
<DIRECTORY>/Install-OpenApiHttpTriggerTemplates.ps1 `
68+
-ProjectPath <PROJECT> ` # Optional parameter. If omitted, it is set to the current directory.
69+
-Namespace <NAMESPACE> ` # Optional parameter. If omitted, it is set to Aliencube.AzureFunctions.Extensions.OpenApi
70+
-IsVersion1 # Optional switch only for Azure Functions v1.
5471
```
5572

56-
> **NOTE**: If you're running v1, add a switch, `-IsVersion1` at the end of the PowerShell script.
73+
Alternatively, run the following script to install the Open API boilerplate templates in Bash shell.
5774

5875
```bash
5976
# Bash script
60-
scripts/Install-OpenApiHttpTriggerTemplates.sh \
61-
./samples/Aliencube.AzureFunctions.FunctionAppV3Static \
62-
Aliencube.AzureFunctions.FunctionAppV3Static
77+
<DIRECTORY>/Install-OpenApiHttpTriggerTemplates.sh \
78+
<PROJECT> \ # Optional argument. If omitted, it is set to the current directory.
79+
<NAMESPACE> # Optional argument. If omitted, it is set to Aliencube.AzureFunctions.Extensions.OpenApi
6380
```
6481

65-
However, if you want to manually integrate by yourself, follow the next steps.
82+
> **NOTE**: Bash script does not support Azure Functions V1.
6683
6784

68-
### Rendering Open API Document ###
85+
### Option 2. Write Open API Boilerplate Codes Manually ###
6986

70-
In order to include an HTTP endpoint in the Open API document, use attribute classes (decorators) like:
87+
Instead of installing the boilerplate code above, you can write the Open API boilerplate codes all by yourself.
7188

72-
```csharp
73-
[FunctionName(nameof(PostSample))]
74-
[OpenApiOperation("add", "sample")]
75-
[OpenApiRequestBody("application/json", typeof(SampleRequestModel))]
76-
[OpenApiResponseBody(HttpStatusCode.OK, "application/json", typeof(SampleResponseModel))]
77-
public static async Task<IActionResult> PostSample(
78-
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "samples")] HttpRequest req,
79-
ILogger log)
80-
{
81-
...
82-
}
83-
```
8489

85-
Then, define another HTTP endpoint to render Swagger document:
90+
#### Open API Document Endpoint - `swagger.json` ####
91+
92+
In order to render the `swagger.json` document, define the HTTP endpoint for it.
8693

8794
```csharp
8895
[FunctionName(nameof(RenderSwaggerDocument))]
@@ -102,8 +109,10 @@ public static async Task<IActionResult> RenderSwaggerDocument(
102109
.AddMetadata(openApiInfo)
103110
.AddServer(req, httpSettings.RoutePrefix)
104111
.Build(Assembly.GetExecutingAssembly(), new CamelCaseNamingStrategy())
112+
// Depending on what you pass, the rendered document can comply Open API v2 or v3, or JSON or YAML.
105113
.RenderAsync(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json)
106114
.ConfigureAwait(false);
115+
107116
var response = new ContentResult()
108117
{
109118
Content = result,
@@ -118,9 +127,9 @@ public static async Task<IActionResult> RenderSwaggerDocument(
118127
> **NOTE**: In order to render payload definitions in `camelCasing`, add `new CamelCaseNamingStrategy()` as an optional argument to the `Build()` method. If this is omitted, payload will be rendered as defined in the payload definitions.
119128
120129

121-
### Rendering Swagger UI ###
130+
#### Swagger UI Page Endpoint ####
122131

123-
In order to render Swagger UI, define another HTTP endpoint for it:
132+
In order to render the Swagger UI page, define the HTTP endpoint for it.
124133

125134
```csharp
126135
[FunctionName(nameof(RenderSwaggerUI))]
@@ -132,7 +141,7 @@ public static async Task<IActionResult> RenderSwaggerUI(
132141
var openApiInfo = OpenApiInfoResolver.Resolve();
133142
var host = HostJsonResolver.Resolve();
134143
var httpSettings = host.GetHttpSettings();
135-
var swaggerAuthKey = ConfigurationResolver.GetValue<string>("OpenApi:ApiKey");
144+
var swaggerAuthKey = Environment.GetEnvironmentVariable("OpenApi__ApiKey");
136145

137146
var ui = new SwaggerUI();
138147
var result = await ui.AddMetadata(openApiInfo)
@@ -152,15 +161,47 @@ public static async Task<IActionResult> RenderSwaggerUI(
152161
```
153162

154163

155-
## Open API Metadata ##
164+
### Expose Endpoints to Open API Document ###
156165

157-
To generate an Open API document, [OpenApiInfo object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#infoObject) needs to be defined. This information can be declared in three places &ndash; `host.json`, `openapisettings.json` or `local.settings.json`.
166+
In order to include HTTP endpoints into the Open API document, use attribute classes (decorators) like:
167+
168+
```csharp
169+
[FunctionName(nameof(PostSample))]
170+
[OpenApiOperation("postSample", "sample")]
171+
[OpenApiRequestBody("application/json", typeof(SampleRequestModel))]
172+
[OpenApiResponseBody(HttpStatusCode.OK, "application/json", typeof(SampleResponseModel))]
173+
public static async Task<IActionResult> PostSample(
174+
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "samples")] HttpRequest req,
175+
ILogger log)
176+
{
177+
...
178+
}
179+
```
180+
181+
182+
### Configure App Settings Key ###
183+
184+
This key is only required if:
185+
186+
* The Function app is deployed to Azure, and
187+
* The Open API related endpoints has the `AuthorizationLevel` value other than `Anonymous`.
188+
189+
If the above conditions are met, add the following key to your `locall.settings.json` or App Settings blade on Azure.
190+
191+
* `OpenApi__ApiKey`: either the host key value or the master key value.
192+
193+
> **NOTE**: It is NOT required if your Open API related endpoints are set to the authorisation level of `Anonymous`.
194+
195+
196+
## Open API Metadata Configuration ##
197+
198+
To generate an Open API document, [OpenApiInfo object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#infoObject) needs to be defined. This information can be declared in **ONE OF THREE** places &ndash; `host.json`, `openapisettings.json` or `local.settings.json`.
158199

159200
This library looks for the information in the following order:
160201

161202
1. `host.json`
162203
2. `openapisettings.json`
163-
3. `local.settings.json` or App Settings blade
204+
3. `local.settings.json` or App Settings blade on Azure
164205

165206

166207
### `host.json` ###
@@ -172,7 +213,7 @@ Although it has not been officially accepted to be a part of `host.json`, the Op
172213
...
173214
"openApi": {
174215
"info": {
175-
"version": "3.0.0",
216+
"version": "1.0.0",
176217
"title": "Open API Sample on Azure Functions",
177218
"description": "A sample API that runs on Azure Functions 3.x using Open API specification - from **host. json**.",
178219
"termsOfService": "https://github.com/aliencube/AzureFunctions.Extensions",
@@ -199,7 +240,7 @@ The Open API metadata can be defined in a separate file, `openapisettings.json`
199240
```json
200241
{
201242
"info": {
202-
"version": "3.0.0",
243+
"version": "1.0.0",
203244
"title": "Open API Sample on Azure Functions",
204245
"description": "A sample API that runs on Azure Functions 3.x using Open API specification - from **openapisettings.json**.",
205246
"termsOfService": "https://github.com/aliencube/AzureFunctions.Extensions",
@@ -216,9 +257,10 @@ The Open API metadata can be defined in a separate file, `openapisettings.json`
216257
}
217258
```
218259

219-
### `local.settings.json` or App Settings ###
220260

221-
On either your `local.settings.json` or App Settings on Azure Functions instance, those details should be set up to render Open API metadata:
261+
### `local.settings.json` or App Settings Blade ###
262+
263+
On either your `local.settings.json` or App Settings on Azure Functions instance, those details can be set up like:
222264

223265
* `OpenApi__Info__Version`: **REQUIRED** Version of Open API document. This is not the version of Open API spec. eg. 1.0.0
224266
* `OpenApi__Info__Title`: **REQUIRED** Title of Open API document. eg. Open API Sample on Azure Functions
@@ -229,9 +271,8 @@ On either your `local.settings.json` or App Settings on Azure Functions instance
229271
* `OpenApi__Info__Contact__Url`: Contact URL. eg. https://github.com/aliencube/AzureFunctions.Extensions/issues
230272
* `OpenApi__Info__License__Name`: **REQUIRED** License name. eg. MIT
231273
* `OpenApi__Info__License__Url`: License URL. eg. http://opensource.org/licenses/MIT
232-
* `OpenApi__ApiKey`: API Key of the endpoint that renders the Open API document.
233274

234-
> **NOTE**: In order to deploy Azure Functions v1 to Azure, the `AzureWebJobsScriptRoot` **MUST** be specified in the app settings section; otherwise it will throw an error that can't find `host.json`. Local debugging is fine, though. For more details, please visit [this page](https://docs.microsoft.com/bs-latn-ba/azure/azure-functions/functions-app-settings#azurewebjobsscriptroot).
275+
> **NOTE**: In order to deploy Azure Functions v1 to Azure, the `AzureWebJobsScriptRoot` **MUST** be specified in the app settings section; otherwise it will throw an error that can't find `host.json`. Local debugging is fine, though. For more details, please visit [this page](https://docs.microsoft.com/azure/azure-functions/functions-app-settings#azurewebjobsscriptroot?WT.mc_id=azfuncextension-github-juyoo).
235276
236277

237278
## Decorators ##

scripts/Install-OpenApiHttpTriggerTemplates.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Param(
2-
[string] [Parameter(Mandatory=$true)] $ProjectPath,
3-
[string] [Parameter(Mandatory=$true)] $Namespace,
2+
[string] [Parameter(Mandatory=$false)] $ProjectPath = ".",
3+
[string] [Parameter(Mandatory=$false)] $Namespace = "Aliencube.AzureFunctions.Extensions.OpenApi",
44
[switch] [Parameter(Mandatory=$false)] $IsVersion1
55
)
66

scripts/Install-OpenApiHttpTriggerTemplates.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
path=$1
44
namespace=$2
55

6+
[ -z "$path" ] && path=.
7+
[ -z "$namespace" ] && namespace=Aliencube.AzureFunctions.Extensions.OpenApi
8+
69
# Checks if directory exists
710
[ ! -d "$path/OpenApi" ] && mkdir -p "$path/OpenApi"
811

0 commit comments

Comments
 (0)