Skip to content

Commit

Permalink
wip: generate swagger and openapi json on build, include ositemindex.…
Browse files Browse the repository at this point in the history
…data xml comments, general doc cleanup
  • Loading branch information
Twinki14 committed Jun 21, 2021
1 parent f4079ca commit 0f8b921
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 46 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"swashbuckle.aspnetcore.cli": {
"version": "6.1.4",
"commands": [
"swagger"
]
}
}
}
17 changes: 14 additions & 3 deletions src/OSItemIndex.API/Controllers/ItemsController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OSItemIndex.API.Models;
using OSItemIndex.API.Services;
using OSItemIndex.Data;

namespace OSItemIndex.API.Controllers
{
Expand All @@ -20,7 +22,8 @@ public ItemsController(IItemsService itemsService)
/// Returns a array of all items in the current database, with optional query filtering
/// </summary>
[HttpGet] // GET: items
public async Task<ActionResult> GetItems([FromQuery] ItemQuery query)
[Produces("application/json")]
public async Task<ActionResult<IEnumerable<OsrsBoxItem>>> GetItems([FromQuery] ItemQuery query)
{
return Ok(await _itemsService.GetItemsAsync(Request.QueryString.HasValue ? query : null));
}
Expand All @@ -31,10 +34,18 @@ public async Task<ActionResult> GetItems([FromQuery] ItemQuery query)
/// <param name="id">Item ID</param>
[HttpGet] // GET: items/{id}
[Route("{id:int}")]
public async Task<ActionResult> GetItem(int id)
[Produces("application/json")]
public async Task<ActionResult<OsrsBoxItem>> GetItem(int id)
{
var result = await _itemsService.GetItemAsync(id);
return Ok(result);
}

[HttpGet]
[Route("/version")] // GET: items/version
public async Task<ActionResult> GetVersion()
{
return Ok();
}
}
}
27 changes: 0 additions & 27 deletions src/OSItemIndex.API/Models/ItemQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,12 @@
{
public class ItemQuery
{
/// <summary>
///
/// </summary>
public string Name { get; set; }

/// <summary>
///
/// </summary>
public bool? LooseCompare { get; set; }

/// <summary>
///
/// </summary>
public bool? Duplicate { get; set; }

/// <summary>
///
/// </summary>
public bool? Noted { get; set; }

/// <summary>
///
/// </summary>
public bool? Placeholder { get; set; }

/// <summary>
///
/// </summary>
public bool? Stackable { get; set; }

/// <summary>
///
/// </summary>
public bool? TradeableOnGe { get; set; }
}
}
20 changes: 10 additions & 10 deletions src/OSItemIndex.API/OSItemIndex.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>C:\Users\Twinki\Documents\Visual Studio Projects\OSItemIndex\OSItemIndex.API\OSItemIndex.API.xml</DocumentationFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -22,14 +19,17 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
</ItemGroup>

<ItemGroup>
<None Update="OSItemIndex.API.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OSItemIndex.Data\OsItemIndex.Data.csproj" />
</ItemGroup>

<Target Name="ToolRestore" AfterTargets="CollectPackageReferences">
<Exec Command="dotnet tool restore" />
</Target>

<Target Name="GenerateSwagger" AfterTargets="AfterBuild">
<Exec Command="dotnet swagger tofile --output openapi.json $(OutputPath)$(AssemblyName).dll v1" />
<Exec Command="dotnet swagger tofile --serializeasv2 --output swagger.json $(OutputPath)$(AssemblyName).dll v1" />
</Target>

</Project>
14 changes: 8 additions & 6 deletions src/OSItemIndex.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.ResponseCompression;
using OSItemIndex.API.Repositories;
using OSItemIndex.API.Services;
Expand Down Expand Up @@ -50,17 +49,20 @@ public void ConfigureServices(IServiceCollection services)

services.AddSwaggerGen(c =>
{
var apiXml = Path.Combine(AppContext.BaseDirectory, "OSItemIndex.API.xml");
var dataXml = Path.Combine(AppContext.BaseDirectory, "OSItemIndex.Data.xml");

c.SwaggerDoc("v1", new OpenApiInfo { Title = "OSItemIndex.API", Version = "v1" });
var filePath = Path.Combine(AppContext.BaseDirectory, "OSItemIndex.API.xml");
c.IncludeXmlComments(filePath);
});
c.IncludeXmlComments(apiXml);
c.IncludeXmlComments(dataXml);
}); // https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-5.0&tabs=visual-studio
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "OSItemIndex.API v1"));
app.UseSwagger(c => { c.SerializeAsV2 = true; });
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "OSItemIndex.API v1"); });

app.UseRouting();
app.UseHttpsRedirection();
Expand Down
183 changes: 183 additions & 0 deletions src/OSItemIndex.API/openapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"openapi": "3.0.1",
"info": {
"title": "OSItemIndex.API",
"version": "v1"
},
"paths": {
"/items": {
"get": {
"tags": [
"Items"
],
"summary": "Returns a array of all items in the current database, with optional query filtering",
"parameters": [
{
"name": "Name",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "LooseCompare",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "Duplicate",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "Noted",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "Placeholder",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "Stackable",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "TradeableOnGe",
"in": "query",
"schema": {
"type": "boolean"
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/OsrsBoxItem"
}
}
}
}
}
}
}
},
"/items/{id}": {
"get": {
"tags": [
"Items"
],
"summary": "Returns an item matching the specified Item ID",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Item ID",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OsrsBoxItem"
}
}
}
}
}
}
},
"/version": {
"get": {
"tags": [
"Items"
],
"responses": {
"200": {
"description": "Success"
}
}
}
}
},
"components": {
"schemas": {
"OsrsBoxItem": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": "Unique OSRS item ID number.",
"format": "int32"
},
"name": {
"type": "string",
"description": "The name of the item.",
"nullable": true
},
"duplicate": {
"type": "boolean",
"description": "If the item is a duplicate.",
"nullable": true
},
"noted": {
"type": "boolean",
"description": "If the item is noted.",
"nullable": true
},
"placeholder": {
"type": "boolean",
"description": "If the item is a placeholder.",
"nullable": true
},
"stackable": {
"type": "boolean",
"description": "If the item is stackable (in inventory).",
"nullable": true
},
"tradeable_on_ge": {
"type": "boolean",
"description": "If the item is tradeable (only on GE).",
"nullable": true
},
"last_updated": {
"type": "string",
"description": "The last time (UTC) the item was updated (in ISO8601 date format).",
"format": "date-time",
"nullable": true
},
"document": {
"type": "string",
"description": "OSRSBox item document.",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}
Loading

0 comments on commit 0f8b921

Please sign in to comment.