-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25800 from phillip-kruger/openapi-js
OpenAPI enhancement
- Loading branch information
Showing
4 changed files
with
141 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...loyment/src/main/java/io/quarkus/smallrye/openapi/deployment/filter/AutoServerFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.quarkus.smallrye.openapi.deployment.filter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.microprofile.openapi.OASFilter; | ||
import org.eclipse.microprofile.openapi.models.OpenAPI; | ||
import org.eclipse.microprofile.openapi.models.servers.Server; | ||
|
||
import io.smallrye.openapi.api.models.servers.ServerImpl; | ||
|
||
/** | ||
* Automatically add default server if none is provided | ||
*/ | ||
public class AutoServerFilter implements OASFilter { | ||
|
||
private static final String DESCRIPTION = "Auto generated value"; | ||
private static final String HTTP = "http"; | ||
private static final String ZEROS = "0.0.0.0"; | ||
private static final String LOCALHOST = "localhost"; | ||
private static final String URL_PATTERN = "%s://%s:%d"; | ||
|
||
private final String defaultScheme; | ||
private final String defaultHost; | ||
private final int defaultPort; | ||
|
||
public AutoServerFilter(String defaultScheme, String defaultHost, int defaultPort) { | ||
if (defaultScheme == null) { | ||
defaultScheme = HTTP; | ||
} | ||
if (defaultHost == null) { | ||
defaultHost = ZEROS; | ||
} | ||
this.defaultScheme = defaultScheme; | ||
this.defaultHost = defaultHost; | ||
this.defaultPort = defaultPort; | ||
} | ||
|
||
@Override | ||
public void filterOpenAPI(OpenAPI openAPI) { | ||
|
||
List<Server> servers = openAPI.getServers(); | ||
if (servers == null || servers.isEmpty()) { | ||
servers = new ArrayList<>(); | ||
|
||
// In case of 0.0.0.0, also add localhost | ||
if (this.defaultHost.equals(ZEROS)) { | ||
ServerImpl localhost = new ServerImpl(); | ||
localhost.setUrl(getUrl(this.defaultScheme, LOCALHOST, this.defaultPort)); | ||
localhost.setDescription(DESCRIPTION); | ||
servers.add(localhost); | ||
} | ||
|
||
ServerImpl serverImpl = new ServerImpl(); | ||
serverImpl.setUrl(getUrl(this.defaultScheme, this.defaultHost, this.defaultPort)); | ||
serverImpl.setDescription(DESCRIPTION); | ||
servers.add(serverImpl); | ||
|
||
openAPI.setServers(servers); | ||
} | ||
} | ||
|
||
private String getUrl(String scheme, String host, int port) { | ||
return String.format(URL_PATTERN, scheme, host, port); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...pi/src/main/java/io/quarkus/smallrye/openapi/deployment/spi/OpenApiDocumentBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.smallrye.openapi.deployment.spi; | ||
|
||
import io.quarkus.builder.item.SimpleBuildItem; | ||
import io.smallrye.openapi.api.OpenApiDocument; | ||
|
||
/** | ||
* The final OpenAPI Document as generated by the Extension. | ||
*/ | ||
public final class OpenApiDocumentBuildItem extends SimpleBuildItem { | ||
|
||
private final OpenApiDocument openApiDocument; | ||
|
||
public OpenApiDocumentBuildItem(OpenApiDocument openApiDocument) { | ||
this.openApiDocument = openApiDocument; | ||
} | ||
|
||
public OpenApiDocument getOpenApiDocument() { | ||
return openApiDocument; | ||
} | ||
} |