Skip to content

Update chapter04.adoc about MicroProfile OpenAPI 4.1 with the latest features from OpenAPI 3.0#63

Open
ttelang wants to merge 4 commits intomicroprofile:mainfrom
ttelang:chatper-update-mp-openapi-4-1
Open

Update chapter04.adoc about MicroProfile OpenAPI 4.1 with the latest features from OpenAPI 3.0#63
ttelang wants to merge 4 commits intomicroprofile:mainfrom
ttelang:chatper-update-mp-openapi-4-1

Conversation

@ttelang
Copy link
Contributor

@ttelang ttelang commented Feb 4, 2026

This PR contains is for updating Chapter 04 of the official MicroProfile 7.1 API Tutorial with the coverage the latest feature and enhancements in MicroProfile OpenAPI 4.1.

This chapter provides detailed coverage of OpenAPI Specification integration with Jakarta RESTful Web Services, focusing on annotation-driven API documentation generation.

Topics Updated

  • Swagger UI Integration: Interactive API exploration and testing
  • Complete Code Examples: Working code examples with explanations
  • MicroProfile OpenAPI 4.1 New Features: Detailed coverage of all new features in version 4.1:
    • OpenAPI v3.1 Specification Support
      • JSON Schema 2020-12 alignment
      • Improved nullable type handling with type arrays
      • Enhanced schema composition (oneOf, anyOf, allOf)
      • Optional<T> support for nullable fields
    • Java Records Support
      • Native documentation generation for Java records
    • Enhanced Annotation Type Safety
      • @Target meta-annotation improvements
      • Compile-time validation
      • IDE integration benefits
    • JSON Schema Dialect Support
      • Default vs. full JSON Schema 2020-12 dialects
      • Programmatic dialect specification
      • CustomModelReader implementation
    • Extensible Interface Methods
      • New hasExtension() and getExtension() convenience methods
      • Vendor extensions documentation
      • ExtensionFilter implementation for dynamic documentation
    • Asynchronous Operations with Callbacks
      • Webhook-style API documentation
      • @Callback and @CallbackOperation usage
    • Comprehensive Security Schemes
      • API Key authentication
      • JWT Bearer token authentication
      • OAuth2 with authorization code flow
      • Security requirement application at operation and class levels

I would be sharing another PR for the associated code changes in next few days.

Improved clarity and readability of the MicroProfile OpenAPI section by rephrasing sentences and enhancing consistency in terminology.
Updated the chapter content to improve clarity and correctness, including rephrasing sentences and fixing typographical errors.
Rephrase and clarify the explanation of the OpenAPI Specification and its benefits, improving readability and consistency.

Use the `mp.openapi.scan.exclude.packages` and `mp.openapi.scan.exclude.classes` configuration properties to exclude specific resources from documentation. This approach proves useful for internal or administrative endpoints you do not want to expose in your public API specification.

== Using MicroProfile OpenAPI in your project
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title for this section is the same as the title for the next section.

Comment on lines +181 to +182
* `@APIResponses`: Documents possible HTTP responses from the operation
* `@APIResponse`: Defines a specific response with status code and content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the example above, you don't actually need the @APIResponses annotation, you can just write the @APIResponse annotation twice on the method:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(summary = "List all products", description = "Retrieves a list of all available products")
    @APIResponse(
        responseCode = "200",
        description = "Successful, list of products found",
        content = @Content(mediaType = "application/json",
                schema = @Schema(implementation = Product.class))
    )
    @APIResponse(
        responseCode = "400",
        description = "Unsuccessful, no products found",
        content = @Content(mediaType = "application/json")
    )
    public List<Product> getAllProducts() {

The only time you need to explicitly include @APIResponses is if you want to use the extensions field.

Comment on lines +188 to 193
Add the following property to the `src/main/resources/META-INF/microprofile-config.properties` file:

[source, properties]
----
mp.openapi.scan=true
----
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit surprising, the spec only defines the mp.openapi.scan.disabled config property, which defaults to false. (i.e. scanning enabled)

Did some implementations need this? If it's needed in some cases, it probably makes sense to keep it in the tutorial.

The `/openapi` endpoint returns the OpenAPI specification generated from the annotations in your source code. It returns information in YAML format.

When we access the `http://localhost:5050/openapi` URL, we should see the API documentation that was generated by MicroProfile OpenAPI:
When you access the `\http://localhost:9080/openapi` URL, you see the API documentation that MicroProfile OpenAPI generated:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the \ before http deliberate here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it stops it being rendered as a hyperlink, that makes sense here.

Comment on lines +352 to +354
== New features in MicroProfile OpenAPI 4.1

MicroProfile OpenAPI 4.1 introduces several enhancements that improve developer productivity and align with the OpenAPI v3.1 specification. These features simplify API documentation, add support for modern Java language features, and provide better schema validation capabilities.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support for OpenAPI v3.1 was actually first included in MP OpenAPI 4.0.

You can reference the Release Notes for more details.

MP OpenAPI 4.1 really didn't add very much, but 4.0 was a major release so I think it is worth calling out these new features, just be aware that they were added in 4.0, not 4.1.

import java.util.LinkedHashMap;
import java.util.Map;

public class ExtensionFilter implements OASFilter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example could be simplified by using the getExtension method added in 4.1, along with the preexisting addExtension method, rather than handling the map of extensions directly.

* Dynamic documentation enrichment: Add computed metadata based on existing extensions
* Validation: Log warnings for operations missing required extensions
* Policy enforcement: Ensure all admin operations have proper security annotations
* Metrics: Count operations by type or collect extension statistics
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The others seem plausible, I'm not sure about this one.

Comment on lines +937 to +939
=== Documenting asynchronous operations with callbacks

MicroProfile OpenAPI 4.1 supports documenting asynchronous operations that use callbacks. Callbacks prove useful for webhook-style APIs where your service initiates an asynchronous process and notifies the client when it completes by making an HTTP request back to a URL provided by the client.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support for callbacks was not new in 4.1 (or 4.0).

@Callback documents a callback that can be set up using an operation.

4.0 added support for webhooks, which document a callback which can be set up via some out of band mechanism (e.g. through a form on a web page). The OpenAPI document may include information on how to set the webhook up.

Comment on lines +1263 to +1265
=== Documenting security schemes

Security is a critical aspect of API documentation. MicroProfile OpenAPI 4.1 provides comprehensive support for documenting various security schemes including API keys, HTTP authentication, OAuth2, and OpenID Connect.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still in the "what's new" section?

If so, we should document what was actually added, otherwise you give the impression that security schemes were new in 4.0, whereas actually it was only a small tweak.

Comment on lines +1328 to +1332
==== Applying security to operations

You can apply security requirements to individual operations or to entire resource classes.

*Applying security to individual operations:*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever we talk about applying security requirements in OpenAPI, we need to be very clear that OpenAPI only documents security requirements, it doesn't enforce anything.

The user must first implement access controls, and then use OpenAPI to document to their end users how to authenticate with the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants