-
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.
Added new Filter to automatically set the server in the OpenAPI Schema
Signed-off-by: Phillip Kruger <[email protected]>
- Loading branch information
1 parent
817dae1
commit b4854bc
Showing
3 changed files
with
123 additions
and
1 deletion.
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
87 changes: 87 additions & 0 deletions
87
...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,87 @@ | ||
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 ZEROS = "0.0.0.0"; | ||
private static final String LOCALHOST = "localhost"; | ||
private static final String URL_PATTERN = "%s://%s:%d"; | ||
|
||
private String defaultScheme; | ||
private String defaultHost; | ||
private int defaultPort; | ||
|
||
public AutoServerFilter() { | ||
|
||
} | ||
|
||
public AutoServerFilter(String defaultScheme, String defaultHost, int defaultPort) { | ||
this.defaultScheme = defaultScheme; | ||
this.defaultHost = defaultHost; | ||
this.defaultPort = defaultPort; | ||
} | ||
|
||
public String getDefaultScheme() { | ||
return defaultScheme; | ||
} | ||
|
||
public void setDefaultScheme(String defaultScheme) { | ||
this.defaultScheme = defaultScheme; | ||
} | ||
|
||
public String getDefaultHost() { | ||
return defaultHost; | ||
} | ||
|
||
public void setDefaultHost(String defaultHost) { | ||
this.defaultHost = defaultHost; | ||
} | ||
|
||
public int getDefaultPort() { | ||
return defaultPort; | ||
} | ||
|
||
public void setDefaultPort(int defaultPort) { | ||
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); | ||
} | ||
} |