use of io.swagger.v3.oas.models.servers.ServerVariable in project swagger-core by swagger-api.
the class Ticket2926Test method testExtensionsInMapDeserializeAndSerialize.
@Test
public void testExtensionsInMapDeserializeAndSerialize() throws Exception {
String yaml = "openapi: 3.0.1\n" + "info:\n" + " title: My title\n" + " description: API under test\n" + " version: 1.0.7\n" + " x-info: test\n" + "servers:\n" + "- url: http://localhost:9999/api\n" + " x-server: test\n" + " description: desc\n" + " variables: \n" + " serVar: \n" + " description: desc\n" + " x-serverVariable: test\n" + "paths:\n" + " /foo/bar:\n" + " get:\n" + " callbacks:\n" + " /foo/bar:\n" + " get:\n" + " description: getoperation\n" + " x-callback: test\n" + " responses:\n" + " default:\n" + " description: it works!\n" + " content:\n" + " application/json:\n" + " schema:\n" + " title: inline_response_200\n" + " type: object\n" + " properties:\n" + " name:\n" + " type: string\n" + " x-mediatype: test\n" + " x-response: test\n" + " x-responses: test\n" + " x-responses-object: \n" + " aaa: bbb\n" + " x-responses-array: \n" + " - aaa\n" + " - bbb\n" + " x-operation: test\n" + " x-pathitem: test\n" + " x-paths: test\n" + "x-openapi-object: \n" + " aaa: bbb\n" + "x-openapi-array: \n" + " - aaa\n" + " - bbb\n" + "x-openapi: test";
OpenAPI aa = Yaml.mapper().readValue(yaml, OpenAPI.class);
SerializationMatchers.assertEqualsToYaml(aa, yaml);
}
use of io.swagger.v3.oas.models.servers.ServerVariable in project swagger-core by swagger-api.
the class AnnotationsUtils method getServer.
public static Optional<Server> getServer(io.swagger.v3.oas.annotations.servers.Server server) {
if (server == null) {
return Optional.empty();
}
Server serverObject = new Server();
boolean isEmpty = true;
if (StringUtils.isNotBlank(server.url())) {
serverObject.setUrl(server.url());
isEmpty = false;
}
if (StringUtils.isNotBlank(server.description())) {
serverObject.setDescription(server.description());
isEmpty = false;
}
if (server.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(server.extensions());
if (extensions != null) {
extensions.forEach(serverObject::addExtension);
}
isEmpty = false;
}
if (isEmpty) {
return Optional.empty();
}
io.swagger.v3.oas.annotations.servers.ServerVariable[] serverVariables = server.variables();
ServerVariables serverVariablesObject = new ServerVariables();
for (io.swagger.v3.oas.annotations.servers.ServerVariable serverVariable : serverVariables) {
ServerVariable serverVariableObject = new ServerVariable();
if (StringUtils.isNotBlank(serverVariable.description())) {
serverVariableObject.setDescription(serverVariable.description());
}
if (StringUtils.isNotBlank(serverVariable.defaultValue())) {
serverVariableObject.setDefault(serverVariable.defaultValue());
}
if (serverVariable.allowableValues() != null && serverVariable.allowableValues().length > 0) {
if (StringUtils.isNotBlank(serverVariable.allowableValues()[0])) {
serverVariableObject.setEnum(Arrays.asList(serverVariable.allowableValues()));
}
}
if (serverVariable.extensions() != null && serverVariable.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(serverVariable.extensions());
if (extensions != null) {
extensions.forEach(serverVariableObject::addExtension);
}
}
serverVariablesObject.addServerVariable(serverVariable.name(), serverVariableObject);
}
serverObject.setVariables(serverVariablesObject);
return Optional.of(serverObject);
}
use of io.swagger.v3.oas.models.servers.ServerVariable in project swagger-parser by swagger-api.
the class OpenAPIDeserializer method getServerVariable.
public ServerVariable getServerVariable(ObjectNode obj, String location, ParseResult result) {
if (obj == null) {
return null;
}
ServerVariable serverVariable = new ServerVariable();
ArrayNode arrayNode = getArray("enum", obj, false, location, result);
if (arrayNode != null) {
List<String> _enum = new ArrayList<>();
for (JsonNode n : arrayNode) {
if (n.isValueNode()) {
_enum.add(n.asText());
serverVariable.setEnum(_enum);
} else {
result.invalidType(location, "enum", "value", n);
}
}
}
String value = getString("default", obj, true, String.format("%s.%s", location, "default"), result);
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
serverVariable.setDefault(value);
}
value = getString("description", obj, false, String.format("%s.%s", location, "description"), result);
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
serverVariable.setDescription(value);
}
Map<String, Object> extensions = getExtensions(obj);
if (extensions != null && extensions.size() > 0) {
serverVariable.setExtensions(extensions);
}
Set<String> keys = getKeys(obj);
for (String key : keys) {
if (!SERVER_VARIABLE_KEYS.contains(key) && !key.startsWith("x-")) {
result.extra(location, key, obj.get(key));
}
}
return serverVariable;
}
use of io.swagger.v3.oas.models.servers.ServerVariable in project openremote by openremote.
the class ManagerWebService method init.
@Override
public void init(Container container) throws Exception {
super.init(container);
String rootRedirectPath = getString(container.getConfig(), ROOT_REDIRECT_PATH, ROOT_REDIRECT_PATH_DEFAULT);
// Modify swagger object mapper to match ours
configureObjectMapper(Json.mapper());
Json.mapper().addMixIn(ServerVariable.class, ServerVariableMixin.class);
// Add swagger resource
OpenAPI oas = new OpenAPI().servers(Collections.singletonList(new Server().url("/api/{realm}/").variables(new ServerVariables().addServerVariable("realm", new ServerVariable()._default("master"))))).schemaRequirement("openid", new SecurityScheme().type(SecurityScheme.Type.OAUTH2).flows(new OAuthFlows().authorizationCode(new OAuthFlow().authorizationUrl("/auth/realms/master/protocol/openid-connect/auth").refreshUrl("/auth/realms/master/protocol/openid-connect/token").tokenUrl("/auth/realms/master/protocol/openid-connect/token")))).security(Collections.singletonList(new SecurityRequirement().addList("openid")));
Info info = new Info().title("OpenRemote Manager REST API").description("This is the documentation for the OpenRemote Manager HTTP REST API. Please see the [wiki](https://github.com/openremote/openremote/wiki) for more info.").contact(new Contact().email("info@openremote.io")).license(new License().name("AGPL 3.0").url("https://www.gnu.org/licenses/agpl-3.0.en.html"));
oas.info(info);
SwaggerConfiguration oasConfig = new SwaggerConfiguration().resourcePackages(Stream.of("org.openremote.model.*").collect(Collectors.toSet())).openAPI(oas);
OpenApiResource openApiResource = new OpenApiResource();
openApiResource.openApiConfiguration(oasConfig);
addApiSingleton(openApiResource);
initialised = true;
ResteasyDeployment resteasyDeployment = createResteasyDeployment(container, getApiClasses(), apiSingletons, true);
// Serve REST API
HttpHandler apiHandler = createApiHandler(container, resteasyDeployment);
if (apiHandler != null) {
// Authenticating requests requires a realm, either we receive this in a header or
// we extract it (e.g. from request path segment) and set it as a header before
// processing the request
HttpHandler baseApiHandler = apiHandler;
apiHandler = exchange -> {
String path = exchange.getRelativePath().substring(API_PATH.length());
Matcher realmSubMatcher = PATTERN_REALM_SUB.matcher(path);
if (!realmSubMatcher.matches()) {
exchange.setStatusCode(NOT_FOUND.getStatusCode());
throw new WebApplicationException(NOT_FOUND);
}
// Extract realm from path and push it into REQUEST_HEADER_REALM header
String realm = realmSubMatcher.group(1);
// Move the realm from path segment to header
exchange.getRequestHeaders().put(HttpString.tryFromString(REALM_PARAM_NAME), realm);
URI url = fromUri(exchange.getRequestURL()).replacePath(realmSubMatcher.group(2)).build();
exchange.setRequestURI(url.toString(), true);
exchange.setRequestPath(url.getPath());
exchange.setRelativePath(url.getPath());
baseApiHandler.handleRequest(exchange);
};
}
// Serve deployment files unsecured (explicitly map deployment folders to request paths)
builtInAppDocRoot = Paths.get(getString(container.getConfig(), APP_DOCROOT, APP_DOCROOT_DEFAULT));
customAppDocRoot = Paths.get(getString(container.getConfig(), CUSTOM_APP_DOCROOT, CUSTOM_APP_DOCROOT_DEFAULT));
HttpHandler defaultHandler = null;
if (Files.isDirectory(customAppDocRoot)) {
HttpHandler customBaseFileHandler = createFileHandler(container, customAppDocRoot, null);
defaultHandler = exchange -> {
if (exchange.getRelativePath().isEmpty() || "/".equals(exchange.getRelativePath())) {
exchange.setRelativePath("/index.html");
}
customBaseFileHandler.handleRequest(exchange);
};
}
PathHandler deploymentHandler = defaultHandler != null ? new PathHandler(defaultHandler) : new PathHandler();
// Serve deployment files
if (Files.isDirectory(builtInAppDocRoot)) {
HttpHandler appBaseFileHandler = createFileHandler(container, builtInAppDocRoot, null);
HttpHandler appFileHandler = exchange -> {
if (exchange.getRelativePath().isEmpty() || "/".equals(exchange.getRelativePath())) {
exchange.setRelativePath("/index.html");
}
// Reinstate the full path
exchange.setRelativePath(exchange.getRequestPath());
appBaseFileHandler.handleRequest(exchange);
};
deploymentHandler.addPrefixPath(MANAGER_APP_PATH, appFileHandler);
deploymentHandler.addPrefixPath(SWAGGER_APP_PATH, appFileHandler);
deploymentHandler.addPrefixPath(CONSOLE_LOADER_APP_PATH, appFileHandler);
deploymentHandler.addPrefixPath(SHARED_PATH, appFileHandler);
}
// Redirect / to default app
if (rootRedirectPath != null) {
getRequestHandlers().add(new RequestHandler("Default app redirect", exchange -> exchange.getRequestPath().equals("/"), exchange -> {
LOG.finer("Handling root request, redirecting client to default app");
new RedirectHandler(redirect(exchange, rootRedirectPath)).handleRequest(exchange);
}));
}
if (apiHandler != null) {
getRequestHandlers().add(pathStartsWithHandler("REST API Handler", API_PATH, apiHandler));
}
// This will try and handle any request that makes it to this handler
getRequestHandlers().add(new RequestHandler("Deployment files", exchange -> true, deploymentHandler));
}
use of io.swagger.v3.oas.models.servers.ServerVariable in project swagger-parser by swagger-api.
the class OpenAPIDeserializer method getServerVariables.
public ServerVariables getServerVariables(ObjectNode obj, String location, ParseResult result) {
ServerVariables serverVariables = new ServerVariables();
if (obj == null) {
return null;
}
Set<String> serverKeys = getKeys(obj);
for (String serverName : serverKeys) {
JsonNode serverValue = obj.get(serverName);
ObjectNode server = (ObjectNode) serverValue;
ServerVariable serverVariable = getServerVariable(server, String.format("%s.%s", location, serverName), result);
serverVariables.addServerVariable(serverName, serverVariable);
}
return serverVariables;
}
Aggregations