liveBook · Manning

Stub

As explained in Automating API Delivery

A controller stub is generated server-side scaffolding for handling operations exposed by an API controller.

A controller stub is generated server-side scaffolding for operations described in an OpenAPI definition. It includes request and response models, routing, and controller logic to complete. Generating it from the definition reduces the risk that implementation work drifts from the published API reference; it does not replace checks against the running API. section 6.0 section 6.1

Use the OpenAPI definition as the starting point for changes:

  • Update the definition when an operation’s request or response requirements change. section 6.1
  • Regenerate the server stub for the chosen language and framework, then implement the operation in the generated controller. section 6.1 section 6.2.1
  • Keep the client SDK and API reference aligned with the same definition, and test whether the running server actually conforms. section 6.0
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

The generation command supplies an OpenAPI file to OpenAPI Generator, selects Spring Boot, and sets the output directory and Java package locations. The generated project includes model and controller files; generation provides a starting point, not the operation’s finished behavior. section 6.2.1

Complete a generated operation

In the Spring Boot example, a developer adds viewProduct to the product controller. The method accepts a UUID path parameter, populates a Product, and returns an HTTP 200 response. A separate exception handler shapes an invalid-UUID request into a 400 error response. These behaviors require implementation beyond generating the stub. section 6.2.1

Do not confuse a controller stub with a stub API. A controller stub is code to build out on the server; a stub API is a limited simulation that returns predefined or uniform responses while consumers develop an integration. section 6.0 section 9.2

How Automating API Delivery covers it

How deep it goes
Defines the generated components and walks through Spring Boot generation and one implemented operation, while leaving runtime conformance to testing.
Examples
Uses an OpenAPI Generator command, generated Maven project files, a product-view controller method, and successful and invalid-request responses.
What it assumes
Assumes an OpenAPI definition and installed generation tools; introduces the generated project files and their roles.
What you can do after
Apply it via OpenAPI Generator to create Spring Boot controller scaffolding, then implement an operation and inspect its responses.
How it connects
Links OpenAPI definitions, generated client SDKs, API reference documentation, and API conformance testing.

Also explained in

Compare how 10 books explain stub →

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.