Skip to main content

Reverse engineering a specification from a solution using GenAI: Part 1

Imagine buying complex furniture, but the instructions are a chaotic pile of sticky notes. That is exactly how programmers feel when trying to connect different web services (like weather apps or payment processors). They usually have to dig through messy documentation just to make two programs talk to each other.

The OpenAPI Specification (OAS) fixes this by acting as a universal, standardised "Instruction Manual."

Why It Matters

OpenAPI acts as a perfect bridge between humans and machines:

  • For Humans: It provides a clear map of what a service does, what information it needs, and what it returns.

  • For Computers: Because the rules are strict and predictable, software can "read" the manual and automatically connect to the service.

What Can You Do With It?

With an OpenAPI blueprint, developers can plug into tools that do the heavy lifting for them:

  • Create Visual Guides: Turn complex code into sleek, interactive websites where users can test the service with the click of a button.

  • Write Code Automatically: Instantly generate the "glue code" needed for apps or servers, saving hours of manual typing.

  • Automate Testing: Let tools read the blueprint to automatically double-check that the software works exactly as promised.

The Big Question: Can AI Work Backwards?

Here is where it gets interesting. If we have existing software but no instruction manual, can we use Generative AI to "reverse-engineer" one just by looking at the code? Doing so would instantly unlock all the automated benefits mentioned above for older projects.

In this post, we are putting this to the test: Can ChatGPT take a zipped folder of existing code and successfully work backwards to write the OpenAPI blueprint?


Step 1: A zipped file for an example Python Flask based simple payroll system (and it is simple): https://github.com/scottturnercanterbury/musical-meme.git


Step 2: Load the zipped file into ChatGPT and the following prompt was used "Unpack this zip file into individual files. Produce an OpenAPI specification based on these files starting with app.py"


It went through the process of unzipping, following the struct , ignoring virtual environment properties, etc.


Produce a YAML filr


Step 3: Using the prompt "Converted the OpenAPI specification in YAML to JSON and show"

{

  "openapi": "3.1.0",

  "info": {

    "title": "Musical Meme Payroll Management System",

    "version": "1.0.0",

    "description": "A simple Flask-based payroll management application for managing employee records with salary tracking. This service is primarily HTML-rendering (server-side templates) and uses form submissions rather than JSON APIs."

  },

  "servers": [

    {

      "url": "http://localhost:5000"

    }

  ],

  "tags": [

    {

      "name": "Employees",

      "description": "Employee payroll records"

    }

  ],

  "paths": {

    "/": {

      "get": {

        "tags": ["Employees"],

        "summary": "List employees",

        "description": "Renders a page listing all employee salary records.",

        "responses": {

          "200": {

            "description": "HTML page containing employee list",

            "content": {

              "text/html": {

                "schema": {

                  "type": "string"

                }

              }

            }

          }

        }

      }

    },

    "/employees/add": {

      "get": {

        "tags": ["Employees"],

        "summary": "Show add employee form",

        "responses": {

          "200": {

            "description": "HTML form to add an employee",

            "content": {

              "text/html": {

                "schema": {

                  "type": "string"

                }

              }

            }

          }

        }

      },

figure 1. An extract of the returned specification.


Not a bad start! Let's check if it is valid 

Using https://swagger.io/  and setting up a new account. We can load it in and then check if it works against the specification (hint:it did)

Figure 2 Swagger testing the specification



figure 3: The schemas produced 


As an experiment, can the process be done in reverse ie. go from specification to code: see next post.


All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Comments

Popular posts from this blog

GenAI Productivity: Ideas to project proposal 1

One of the ways I use Generative AI with students is to take basic ideas for projects, usually a title, and get these tools to greater ideas and start of a project proposal. This is with all the usual caveats  Check the references (if any); It is going to be basic, so extend it. In this example I am going to use Co-pilot but the ChatGPT, etc can be used, employing a few basic prompt engineering basics: personas (who is the target audience?) and Templates (how do I want it to look?) to start this process. Example:  Project ideas for MSc Data Intelligence students (persona)  on a particular topic. The reply will include subheadings and relevant (hopefully) content for  TITLE, INTRODUCTION, PROBLEM STATEMENT. The prompt: " Taking the topic "Leveraging open-source tools to measure and present academics publications automatically from public domain data.". Give five innovative projects for a Master's level student dissertation in Data Intelligence. Each project example wi...

GenAI Productivity: Ideas to project proposal ideas from Google Scholar

Generated as well by Google Gemini In previous blog posts, I have looked at generating project ideas for project ideas. These can be used with many different GenAI platforms . As an idea/challenge, I want to come up with a way for students to generate ideas for projects based on knowing who they would like to work with and that person's Google Scholar profile. Here is the catch: often, it can be difficult to access Google Scholar to do this. One solution  is to use Google Gemini, a Google product, to access another Google product. Going to apply this to my own profile - ego rides again . It went and did much when I click on it and deep research, it did a full research report. Prompt used :  " Using this as a starting point https://scholar.google.com/citations?user=ghQedZAAAAAJ&hl=en from the research here provide 10 project ideas suitable for a final year Computer Science student project with this supervisor. For each provide title, 100 word summary, possible outcome s...

Getting multiple viewpoints with ChatGPT

Well sort of! There are approaches where we can get the generative AI to look at a problem from multiple perspectives (or personas) and bring the ideas generated, ideally informed by the others. to a final plan. One of the main strategy is called Tree of Thoughts (see here for more detail  https://www.forbes.com/sites/lanceeliot/2023/09/08/prompt-engineering-embraces-tree-of-thoughts-as-latest-new-technique-to-solve-generative-ai-toughest-problems/?sh=5ce79bdb2c8b ). The central idea is get a number of expert opinions, allow potential cross-fertilization of ideas, come up with actions or plans. Let see this action.  Scenario: Find out about the UK Government's plans on Disability support and then use Tree of Thoughts to produce some ideas for a company making disability equipment based on their website. Google's Gemini will be used. Stage 1 "UK Governments plans on Disability support ": Prompt:  Read, convert to plain text and consolidate information from the followi...