Skip to content

Commit b009bfc

Browse files
author
vp
committed
Improve README with OpenAPI setup and testing details
1 parent 5ce7d6d commit b009bfc

1 file changed

Lines changed: 66 additions & 4 deletions

File tree

README.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,70 @@ public class CoreModuleMapperRegister : IRegister
345345
}
346346
```
347347

348-
### Testing the API
348+
Perfect 🚀 — let’s make it **short and developer‑oriented**, but also **informative** about how the OpenAPI part is setup (MSBuild, package references, and post‑build step).
349349

350-
#### Swagger UI
350+
Here’s a **drop‑in section** for your README that matches the style of what you already have:
351+
352+
---
353+
354+
### OpenAPI Specification and Swagger UI
355+
356+
The project uses **build‑time OpenAPI** documentation creation.
357+
358+
- On **build**, the OpenAPI specification is generated to `wwwroot/openapi.json`.
359+
- ASP.NET Core serves this as a **static file** at [https://localhost:7144/openapi.json](https://localhost:7144/openapi.json).
360+
- **Swagger UI** ([https://localhost:7144/swagger/index.html](https://localhost:7144/swagger/index.html)) is configured to use the generated specification.
361+
362+
This ensures the specification is **consistent across environments** and available as a build artifact.
363+
364+
#### Setup
365+
366+
- **Packages used**:
367+
- `Microsoft.AspNetCore.OpenApi`
368+
- `Microsoft.Extensions.ApiDescription.Server`
369+
- `Swashbuckle.AspNetCore.SwaggerUI` (for the UI only)
370+
371+
- **Project file configuration** (`Presentation.Web.Server.csproj`):
372+
```xml
373+
<PropertyGroup>
374+
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
375+
<OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)/wwwroot</OpenApiDocumentsDirectory>
376+
<OpenApiGenerateDocumentsOptions>--file-name openapi</OpenApiGenerateDocumentsOptions>
377+
</PropertyGroup>
378+
<Target Name="GenerateOpenApiAfterBuild" AfterTargets="Build" DependsOnTargets="GenerateOpenApiDocuments" />
379+
```
380+
381+
- **OpenApi services and static files** (`Program.cs`):
382+
```csharp
383+
builder.Services.AddEndpointsApiExplorer();
384+
builder.Services.AddOpenApi();
385+
...
386+
app.UseSwaggerUI(o =>
387+
{
388+
o.SwaggerEndpoint("/openapi.json", "v1");
389+
app.MapOpenApi();
390+
});
391+
app.UseStaticFiles();
392+
```
351393

352-
Start the application (CTRL-F5) and test via [Swagger UI](https://localhost:7144/swagger/index.html).
394+
395+
This ensures `openapi.json` is automatically created **after each build**. More information can be found in the [Microsoft Docs](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/aspnetcore-openapi#generate-openapi-documents-at-build-time).
396+
397+
#### Client generation
398+
399+
The generated specification can be used to build strongly‑typed clients:
400+
401+
```bash
402+
# TypeScript client
403+
nswag openapi2tsclient /input:src/Presentation.Web.Server/wwwroot/openapi.json /output:Client.ts
404+
405+
# C# client
406+
nswag openapi2csclient /input:src/Presentation.Web.Server/wwwroot/openapi.json /output:Client.cs
407+
```
408+
409+
Since Swagger UI serves the exact same specification as a static asset, it can be targeted at the following endpoint: `https://localhost:7144/openapi.json`
410+
411+
### Testing
353412

354413
#### Unit Tests
355414

@@ -361,9 +420,12 @@ Run tests in `CoreModule.IntegrationTests` (e.g., CustomerEndpointTests for HTTP
361420

362421
#### HTTP Tests
363422

364-
Use tools like Postman or VS HTTP file to test endpoints:
423+
Use tools like Bruno/Postman or VS HTTP file to test endpoints:
365424
- GET /api/core/customers
366425
- POST /api/core/customers with body { "firstName": "John", "lastName": "Doe", "email": "john.doe@example.com" }
426+
427+
A few example requests are in [Core-API.http](./src/Modules/CoreModule/Core-API.http).
428+
367429
---
368430
## License
369431

0 commit comments

Comments
 (0)