Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a section about openapi filters in the doc #39574

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/src/main/asciidoc/openapi-swaggerui.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,49 @@
quarkus.smallrye-openapi.info-license-url=https://www.apache.org/licenses/LICENSE-2.0.html
----

This will give you similar information as the `@OpenAPIDefinition` example above.

Check warning on line 323 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'. Raw Output: {"message": "[Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'.", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 323, "column": 40}}}, "severity": "INFO"}

== Enhancing the OpenAPI Schema with Filters

Check warning on line 325 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in 'Enhancing the OpenAPI Schema with Filters'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in 'Enhancing the OpenAPI Schema with Filters'.", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 325, "column": 4}}}, "severity": "INFO"}

You can change the Generated OpenAPI Schema using one or more filter. Filters can run during build time, or at runtime.

Check warning on line 327 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsSuggestions] Depending on the context, consider using 'by using' or 'that uses' rather than 'using'. Raw Output: {"message": "[Quarkus.TermsSuggestions] Depending on the context, consider using 'by using' or 'that uses' rather than 'using'.", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 327, "column": 44}}}, "severity": "INFO"}

[source,java]
----
/**
* Filter to add custom elements
*/
@OpenApiFilter(OpenApiFilter.RunStage.BUILD) //<1>
public class MyBuildTimeFilter implements OASFilter { //<2>

private IndexView view;

public MyBuildTimeFilter(IndexView view) { //<3>
this.view = view;
}

@Override
public void filterOpenAPI(OpenAPI openAPI) { //<4>
Collection<ClassInfo> knownClasses = this.view.getKnownClasses();
Info info = OASFactory.createInfo();
info.setDescription("Created from Annotated Buildtime filter with " + knownClasses.size() + " known indexed classes");
openAPI.setInfo(info);
}

}
----
<1> Annotate method with `@OpenApiFilter` and the run stage (BUILD,RUN,BOTH)
<2> Implement OASFilter and override any of the methods

Check warning on line 354 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Spelling] Use correct American English spelling. Did you really mean 'OASFilter'? Raw Output: {"message": "[Quarkus.Spelling] Use correct American English spelling. Did you really mean 'OASFilter'?", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 354, "column": 15}}}, "severity": "WARNING"}
<3> For Build stage filters, the index can be passed in (optional)
<4> Get a hold of the generated `OpenAPI` Schema, and enhance as required

Check warning on line 356 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'. Raw Output: {"message": "[Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'.", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 356, "column": 63}}}, "severity": "INFO"}

Remember that setting fields on the schema will override what has been generated, you might want to get and add to (so modify). Also know that the generated values might be null, so you will have to check for that.

=== Runtime filters

Runtime filters by default runs on startup (when the final OpenAPI document gets created). You can change runtime filters to run on every request, making the openapi document dynamic.
To do this you need to set this propery: `quarkus.smallrye-openapi.always-run-filter=true`.

Check warning on line 363 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'need to'. Raw Output: {"message": "[Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'need to'.", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 363, "column": 5}}}, "severity": "INFO"}

Check warning on line 363 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Spelling] Use correct American English spelling. Did you really mean 'propery'? Raw Output: {"message": "[Quarkus.Spelling] Use correct American English spelling. Did you really mean 'propery'?", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 363, "column": 22}}}, "severity": "WARNING"}

== Loading OpenAPI Schema From Static Files

Check warning on line 365 in docs/src/main/asciidoc/openapi-swaggerui.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in 'Loading OpenAPI Schema From Static Files'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in 'Loading OpenAPI Schema From Static Files'.", "location": {"path": "docs/src/main/asciidoc/openapi-swaggerui.adoc", "range": {"start": {"line": 365, "column": 4}}}, "severity": "INFO"}

Instead of dynamically creating OpenAPI schemas from annotation scanning, Quarkus also supports serving static OpenAPI documents.
The static file to serve must be a valid document conforming to the https://swagger.io/docs/specification[OpenAPI specification].
Expand Down
Loading