Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Your team must choose between code-first (springdoc generates the spec from controllers) and design-first (write OpenAPI YAML, generate server interfaces with openapi-generator). What are the trade-offs and what would you pick for a partner API?

Code-first, where springdoc generates the spec from controllers, is fast and always matches the code, but the contract changes whenever the code changes, so breaking changes can slip in unnoticed. Design-first, where we write the OpenAPI YAML and generate server interfaces with openapi-generator, makes the contract reviewable before implementation and lets clients start in parallel, though generated code can be awkward. For a partner API I would pick design-first with review; in both approaches an OpenAPI diff in CI catches breaking changes, and generated interfaces stay thin.

Why this question is asked

Every team that publishes an API must decide what the source of truth is: the code, from which the contract is derived, or the contract, from which code is derived. The choice affects who can review the API, how early clients can start and how easily breaking changes escape. Interviewers ask this scenario because both approaches are widely used and the right answer depends on the consumers. In this answer, we will describe both workflows, compare their costs, see how to keep generated code from taking over the business code, add the safety net both need, and make a choice for a partner API.

1The Two Workflows

In code-first, developers write Spring controllers and DTOs as usual, and springdoc generates the OpenAPI document from them, at runtime or during the build. The contract is a by-product of the implementation.

In design-first, also called contract-first, the team writes the OpenAPI YAML file before any code. The file is reviewed like a design document, possibly by the consumers. Then openapi-generator, with its spring generator, produces Java interfaces and model classes from it, and developers implement those interfaces. The contract is the input; the implementation must conform to it.

Code-first versus design-first
AspectCode-first (springdoc)Design-first (openapi-generator)
Source of truthcontrollers and DTOsthe OpenAPI YAML
Speed to first versionfast, nothing extra to writeslower, spec written first
Spec matches codealways, by constructionenforced by generated interfaces
Review before buildinghard, the API exists only as codeeasy, the YAML is reviewed
Clients start in parallelafter the code existsas soon as the spec is agreed
Main riskaccidental breaking changesawkward generated code, drift

2What Code-First Costs

Code-first is the fastest way to a documented API, and its document can never describe an endpoint that does not exist. But because the contract follows the code, any code change is a contract change. Renaming a DTO field from customerId to clientId during a refactoring silently renames it in the API. Changing a field from int to long, or making a parameter required, ships the moment the code ships. Reviewers looking at a Java diff may not notice that they are approving a breaking API change.

It is also hard for consumers to take part. They see the API only after it is implemented, so feedback arrives when changing it is already expensive.

3What Design-First Costs

Design-first turns the API into an artefact that product managers, consumers and API reviewers can read before anyone writes Java. Clients can generate SDKs and mock servers from the agreed spec and start building at the same time as the server team. Changes to the contract are visible as changes to one YAML file.

The costs are the YAML itself, which is verbose, and the generated code. Generators produce interfaces, models and sometimes controllers whose style may not match the team's, and regenerating can overwrite customisations. So we configure the generator carefully.

XML
<!-- openapi-generator-maven-plugin (excerpt) -->
<configuration>
<inputSpec>${project.basedir}/api/orders.yaml</inputSpec>
<generatorName>spring</generatorName>
<configOptions>
<interfaceOnly>true</interfaceOnly> <!-- only interfaces and models, no controllers -->
<useSpringBoot3>true</useSpringBoot3> <!-- check the option for your Boot version -->
<useTags>true</useTags>
</configOptions>
</configuration>
// generated at build time, never edited:
public interface OrdersApi {
@PostMapping("/orders") ResponseEntity<OrderDto> createOrder(@Valid @RequestBody CreateOrderRequest req);
}
// written by us:
@RestController
class OrdersController implements OrdersApi {
private final OrderService service;
OrdersController(OrderService service) { this.service = service; }
public ResponseEntity<OrderDto> createOrder(CreateOrderRequest req) {
return ResponseEntity.status(201).body(OrderMapper.toDto(service.create(OrderMapper.toCommand(req))));
}
}

Notice that the controller is a thin adapter: it maps the generated request model to our own command, calls the service, and maps back. Business code never uses generated classes, so a generator upgrade or a regenerated model touches only the mappers. And because the controller implements the generated interface, the build fails if the spec changes and the code no longer matches it.

4The Safety Net Both Need: an OpenAPI Diff in CI

Whichever approach we choose, the most valuable single check is an automated comparison between the spec about to be released and the last released one. Tools such as oasdiff or openapi-diff classify differences as additive or breaking: a removed field, a changed type, a new required parameter. In CI, a breaking difference fails the build unless the change is explicitly approved and versioned.

Step throughRelease check for the API contract
  1. Produce the specspringdoc export or the design YAML
  2. Lint against the guidelinenaming, errors, pagination
  3. Diff with the last released specoasdiff or openapi-diff
  4. Breaking change found?
    YesRequire approval and a version planNoPublish the spec and release

4 steps in order. Play them, or step through them one at a time.

5What I Would Pick for a Partner API

For a partner API, I would choose design-first with review. Partners are external, slow to change and often contractually bound to the API, so the cost of an accidental breaking change is high and the value of reviewing the contract before building it is high too. Design-first also lets partners start integrating against a mock server while we implement. We would keep generated interfaces thin and run the diff in CI.

For internal APIs consumed by teams we can talk to, code-first with springdoc plus the CI diff and contract tests is usually enough, and faster. The deciding factor is the cost of breaking a consumer, not a preference for one tool.

6Follow-up questions

How do you keep a design-first spec and the implementation from drifting?

First, generate the server interfaces from the spec in every build and have controllers implement them, so a spec change the code does not match fails compilation. Second, validate behaviour in tests: integration tests can check real responses against the spec with a request and response validator, which catches things interfaces cannot, such as a missing field or a wrong status. Third, some teams also generate the spec from the running code with springdoc and diff it against the design spec in CI, failing on any difference.

Can a team mix the two approaches?

Yes. A common mix is to design new endpoints first in YAML for review, then implement them code-first and use the CI diff to check that the generated spec matches the reviewed one. What matters is that one artefact is declared the source of truth for each API.

Does design-first slow teams down?

Slightly at the start, because the spec must be written and reviewed. It often saves time overall for external APIs, because consumers work in parallel and fewer changes are needed after release.

7Conclusion

In this answer, we compared code-first and design-first OpenAPI workflows. Code-first with springdoc is fast and always matches the code but lets breaking changes slip in with refactorings; design-first with openapi-generator makes the contract reviewable and lets clients start in parallel, at the cost of YAML and generated code. Thin generated interfaces keep business code independent, an OpenAPI diff in CI protects both approaches, and for a partner API design-first with review is the safer choice.

What to say in the interviewAbout 80 seconds aloud

Open with

Code-first means we write controllers and springdoc generates the spec. It is fast and the spec always matches the code, but the contract changes whenever the code changes, so a refactoring that renames a DTO field silently breaks clients, and consumers only see the API after it is built.

Then, if they want more

Design-first means we write the OpenAPI YAML first, review it, possibly with the consumers, and generate server interfaces and models with openapi-generator. The contract is reviewable before any code exists, clients can start against mocks in parallel, and contract changes are visible in one file, but the YAML is verbose and generated code can be awkward.

To manage that, I would generate interfaces only, have thin controllers implement them and map to our own domain types, so business code never depends on generator output. In both approaches I would run an OpenAPI diff, such as oasdiff, in CI against the last released spec and require approval for breaking changes.

For a partner API I would choose design-first with review, because partners are slow to change and a breaking change is expensive. For internal APIs, code-first plus the diff and contract tests is usually enough.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your team must choose between code-first (springdoc generates the spec from controllers)...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/942fdd2933/