liveBook · Manning

Code generation

As explained in Automating API Delivery

Code generation is the automated production of source code or scaffolding from a formal specification or other input.

When an API definition and its implementation are maintained separately, they can drift apart. Code generation reduces that risk by treating the OpenAPI definition as the source of truth (SOT): define the API first, then generate service scaffolding from it. The generated controller, routing, request, and response code provides a starting point for implementation, not a replacement for it. section 6.2

For a Java Spring Boot service, OpenAPI Generator can create a project from an OpenAPI file. Its CLI command identifies the input specification, selects the Spring generator, names the output directory, and sets packages for controllers, models, and configuration. The CLI is a Node application that downloads and runs the generator's Java executable. section 6.2.1

Listing 6.1 Generating a Spring Boot application from OpenAPI
openapi-generator-cli generate
 --input-spec product-catalog-v1-0.oas.yaml    #1
 --generator-name spring                #2
 --output product-catalog-service
 --additional-properties=                    #3
 library=spring-boot,                                  #4
 apiPackage=com.acmepetsupplies.controller,         #5
 modelPackage=com.acmepetsupplies.model,      #6
 configPackage=com.acmepetsupplies.configuration,    #7
 basePackage=com.acmepetsupplies,      #8
 useTags=true        #9

Use this command as the generation template: change the input file, output directory, and package names for your service. Keep the comma-separated additional properties together without spaces between them. section 6.2.1

Inspect the output before adding behavior. The project includes build and configuration files, generated model classes, and API controller files. Its README identifies the generated project, while openapitools.json records the generator package version used. Generated API interfaces carry operation and response annotations; the application still needs implementation of the requested behavior. section 6.2.1

Implement and check a product lookup

A viewProduct controller method can accept a UUID path parameter, populate a generated Product model, and return it with HTTP 200. A request made with curl can then display the product JSON. A non-UUID identifier instead produces an HTTP 400 error response in the completed example. These checks distinguish a generated route and model from a working endpoint with error handling. section 6.2.1

When requirements change, update the SOT first and regenerate the affected artifacts, which may include server code, client SDKs, and reference documentation. Teams can work on those artifacts in parallel once they agree on the definition. Keep a forward-looking “to-be” definition distinct from the “as-is” definition that describes what the service currently implements; a design change is not implemented merely because it appears in the specification. section 6.2.7

How Automating API Delivery covers it

How deep it goes
Walks through a Spring Boot generation command, generated files, and controller behavior without teaching Java or prescribing Spring Boot project structure.
Examples
Uses a generated Spring Boot product-catalog service, a product lookup controller, curl responses, and an invalid-UUID error response.
What it assumes
Provides runnable completed examples for readers unfamiliar with Java and Spring Boot, while assuming access to the OpenAPI Generator CLI.
What you can do after
Apply it via OpenAPI Generator CLI to generate Spring Boot service stubs, implement endpoint behavior, and regenerate after specification changes.
How it connects
Connects code generation to OpenAPI, API conformance, source-of-truth management, client SDKs, reference documentation, and iterative API design.

Also explained in

Compare how 17 books explain code generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Automating API Delivery. The section links point to the parts of the book it draws on.