I want to create a mock server for front end developers of our team.
Here is my sample api-y.yaml
file:
openapi: 3.0.0
info:
title: Mock API
description: A simple mock API
version: 1.0.0
paths:
/api/hello:
get:
summary: Returns a hello message
responses:
'200':
description: A hello message
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: Hello, world!
program.cs
:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
What else should I do in order to see /api/hello
endpoint in Swagger when I run my project?
I tried sample code I found online and I got from ChatGPT but I wasn’t successful
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(options =>
{
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "api-j.yaml"));
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
I also tried using JSON file:
{
"openapi": "3.0.0",
"info": {
"title": "Mock API",
"description": "A simple mock API",
"version": "1.0.0"
},
"paths": {
"/api/hello": {
"get": {
"summary": "Returns a hello message",
"responses": {
"200": {
"description": "A hello message",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"message": {
"type": "string",
"example": "Hello, world!"
}
}
}
}
}
}
}
}
}
}
}
1