Skip to content

Instantly share code, notes, and snippets.

@atjsh
Last active July 15, 2025 04:04
Show Gist options
  • Save atjsh/4a2a6ebd45e6d14afe712ddb7fed9241 to your computer and use it in GitHub Desktop.
Save atjsh/4a2a6ebd45e6d14afe712ddb7fed9241 to your computer and use it in GitHub Desktop.
Generating OpenAPI Swagger Documentation from Source Code, with GPT-4.1 (feedback welcomed)

Generating OpenAPI Swagger Documentation from Source Code, with GPT-4.1

written by atjsh

Idea

which means, chatgpt can generate swagger documentation based on source code..?

Demo

i grabbed a simple API controller source code from github.

it's written in typescript.

source

import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
import { Roles } from '../common/decorators/roles.decorator';
import { RolesGuard } from '../common/guards/roles.guard';
import { ParseIntPipe } from '../common/pipes/parse-int.pipe';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
import { Cat } from './interfaces/cat.interface';

@UseGuards(RolesGuard)
@Controller('cats')
export class CatsController {
  constructor(private readonly catsService: CatsService) {}

  @Post()
  @Roles(['admin'])
  async create(@Body() createCatDto: CreateCatDto) {
    this.catsService.create(createCatDto);
  }

  @Get()
  async findAll(): Promise<Cat[]> {
    return this.catsService.findAll();
  }

  @Get(':id')
  findOne(
    @Param('id', new ParseIntPipe())
    id: number,
  ) {
    // Retrieve a Cat instance by ID
    console.log(id);
  }
}

and we use chatgpt to generate swagger documentation, from the source code.

Setup

  • user input: the source code, as-is
  • configuration
    • temperature: 1.00
    • max tokens: 32768
    • top p: 1.00
    • response: json_schema

the json schema used in the setup can be found in this github gist, 2_structured_output_json_schema.json. it's partial json schema of OpenAPI 3.0.

Result

Screenshot 2025-07-13 at 7 39 31 PM

it did generate valid swagger documentation.

you can check the actual json generated by chatgpt in this github gist, 3_generated_swagger_1.json.

About Hallucination / "the code was included in training set"

however, i did not included a payload information of HTTP body or "DTO"s. but as you can see, chatgpt just somehow generated these info.

i strongly suspect that the source code was already known by chatgpt.

Still, AI can generate OpenAPI Swagger Documentation

Try yourself! you can reproduce this in OpenAI playground.

just turn on "Structured Output" mode, and set the schema with 2_structured_output_json_schema.json.

and chatgpt will generate swagger documentation for you.

What's the benefit of this

NestJS, the framework used in this demo's source code already has support for automatic swagger generation. so why use chatgpt to generate swagger?

i think using chatgpt for api doc generation can be very useful when your source code has no such support. also, chatgpt is a powerful and unversal tool that can understand code.

if your source code is written in popular languages / framework / library, chatgpt will generate (almost) perfect api documentation for free.

Going further

you can do more extream documentation generation by including every source code of your API server and compressing them with prompt compressors, like LLMLingua-2 by Microsoft (i made one that runs in your browser) or simply bundling them with tools like esbuild (i tried it), and use it as LLM input.

That way, you might be able to generate complete API documentation. of course, you have to check for errors made by llm!

Resources

Other articles related to Structured Output

Typos

i speak in Korean so there might be some grammer errors. hope you got the idea.

Discuss

Hacker News

GeekNews

{
"name": "openapi3",
"strict": false,
"description": null,
"schema": {
"description": "The description of OpenAPI v3.0.x Documents",
"type": "object",
"required": [
"openapi",
"info",
"paths"
],
"properties": {
"openapi": {
"description": "@pattern ^3\\.0\\.\\d(-.+)?$",
"type": "string"
},
"info": {
"$ref": "#/$defs/Info"
},
"externalDocs": {
"$ref": "#/$defs/ExternalDocumentatio2n"
},
"servers": {
"type": "array",
"items": {
"$ref": "#/$defs/Server"
}
},
"security": {
"type": "array",
"items": {
"$ref": "#/$defs/SecurityRequirement"
}
},
"tags": {
"description": "@uniqueItems",
"type": "array",
"items": {
"$ref": "#/$defs/Tag"
}
},
"paths": {
"$ref": "#/$defs/Paths"
},
"components": {
"$ref": "#/$defs/Components"
}
}
}
}
{
"openapi": "3.0.3",
"info": {
"title": "Cats API",
"version": "1.0.0",
"description": "OpenAPI spec for the NestJS CatsController"
},
"paths": {
"/cats": {
"get": {
"tags": [
"cat"
],
"summary": "Find all cats",
"operationId": "findAllCats",
"responses": {
"200": {
"description": "List of cats",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Cat"
}
}
}
}
}
}
},
"post": {
"tags": [
"cat"
],
"summary": "Create a cat",
"operationId": "createCat",
"security": [
{
"RolesGuard": []
}
],
"requestBody": {
"description": "Data to create a new cat",
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateCatDto"
}
}
}
},
"responses": {
"201": {
"description": "Cat created"
}
}
}
},
"/cats/{id}": {
"get": {
"tags": [
"cat"
],
"summary": "Find one cat by id",
"operationId": "findOneCat",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "A single cat",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Cat"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Cat": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"breed": {
"type": "string"
}
},
"required": [
"id",
"name",
"age",
"breed"
]
},
"CreateCatDto": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"breed": {
"type": "string"
}
},
"required": [
"name",
"age",
"breed"
]
}
},
"securitySchemes": {
"RolesGuard": {
"type": "apiKey",
"in": "header",
"name": "Authorization"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment