use of io.swagger.v3.oas.annotations.headers.Header 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.annotations.headers.Header in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testExampleVsExamples.
@Test
public void testExampleVsExamples() {
String json = "{" + "\"openapi\": \"3.0.0\"," + "\"info\": {\"title\": \"Examples\", \"version\": \"0.0.0\"}," + "\"paths\": {}," + "\"components\": {" + " \"parameters\": {" + " \"withExample\": {" + " \"name\": \"withExample\"," + " \"in\": \"query\"," + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}," + " \"withExamples\": {" + " \"name\": \"withExamples\"," + " \"in\": \"query\"," + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," + " \"withBoth\": {" + " \"name\": \"withBoth\"," + " \"in\": \"query\"," + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," + " \"example\": \"Hello\"}," + " \"withContentExample\": {" + " \"name\": \"withContentExample\"," + " \"in\": \"query\"," + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}}}," + " \"withContentExamples\": {" + " \"name\": \"withContentExamples\"," + " \"in\": \"query\"," + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," + " \"withContentBoth\": {" + " \"name\": \"withContentBoth\"," + " \"in\": \"query\"," + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}," + " \"headers\": {" + " \"withExample\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}," + " \"withExamples\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," + " \"withBoth\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," + " \"example\": \"Hello\"}}," + " \"requestBodies\": {" + " \"withBodyExample\": {" + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}}}," + " \"withBodyExamples\": {" + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," + " \"withBodyBoth\": {" + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}}}";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(json, null, null);
assertEqualsNoOrder(result.getMessages().toArray(), new Object[] { "attribute components.parameters.withBoth.[withBoth].examples already defined -- ignoring \"example\" field", "attribute components.parameters.withContentBoth.[withContentBoth].content.'application/json'.examples already defined -- ignoring \"example\" field", "attribute components.requestBodies.withBodyBoth.content.'application/json'.examples already defined -- ignoring \"example\" field", "attribute components.headers.withBoth.examples already defined -- ignoring \"example\" field" }, "Expected warnings not found");
OpenAPI openAPI = result.getOpenAPI();
Parameter param;
param = openAPI.getComponents().getParameters().get("withExample");
assertNull(param.getExamples(), "Examples,");
assertNotNull(param.getExample(), "Example,");
param = openAPI.getComponents().getParameters().get("withExamples");
assertNotNull(param.getExamples(), "Examples,");
assertNull(param.getExample(), "Example,");
param = openAPI.getComponents().getParameters().get("withBoth");
assertNotNull(param.getExamples(), "Examples,");
assertNull(param.getExample(), "Example,");
Header header;
header = openAPI.getComponents().getHeaders().get("withExample");
assertNull(header.getExamples(), "Examples,");
assertNotNull(header.getExample(), "Example,");
header = openAPI.getComponents().getHeaders().get("withExamples");
assertNotNull(header.getExamples(), "Examples,");
assertNull(header.getExample(), "Example,");
header = openAPI.getComponents().getHeaders().get("withBoth");
assertNotNull(header.getExamples(), "Examples,");
assertNull(header.getExample(), "Example,");
MediaType mediaType;
mediaType = openAPI.getComponents().getParameters().get("withContentExample").getContent().get("application/json");
assertNull(mediaType.getExamples(), "Examples,");
assertNotNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getParameters().get("withContentExamples").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getParameters().get("withContentBoth").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExample").getContent().get("application/json");
assertNull(mediaType.getExamples(), "Examples,");
assertNotNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExamples").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyBoth").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
}
use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testExamples.
@Test
public void testExamples(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: 3.0.1\n" + "info:\n" + " title: httpbin\n" + " version: 0.0.0\n" + "servers:\n" + " - url: http://httpbin.org\n" + "paths:\n" + " /post:\n" + " post:\n" + " summary: Returns the POSTed data\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/AnyValue'\n" + " examples:\n" + " AnObject:\n" + " $ref: '#/components/examples/AnObject'\n" + " ANull:\n" + " $ref: '#/components/examples/ANull'\n" + " application/yaml:\n" + " schema:\n" + " $ref: '#/components/schemas/AnyValue'\n" + " examples:\n" + " AString:\n" + " $ref: '#/components/examples/AString'\n" + " AnArray:\n" + " $ref: '#/components/examples/AnArray'\n" + " text/plain:\n" + " schema:\n" + " type: string\n" + " example: Hi there\n" + " application/x-www-form-urlencoded:\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " name:\n" + " type: string\n" + " example:\n" + " id: 42\n" + " name: Arthur Dent\n" + " responses:\n" + " '200':\n" + " description: OK\n" + " content:\n" + " application/json:\n" + " schema:\n" + " type: object\n" + "\n" + " #/response-headers:\n" + " /:\n" + " get:\n" + " summary: Returns a response with the specified headers\n" + " parameters:\n" + " - in: header\n" + " name: Server\n" + " required: true\n" + " schema:\n" + " type: string\n" + " examples:\n" + " httpbin:\n" + " value: httpbin\n" + " unicorn:\n" + " value: unicorn\n" + " - in: header\n" + " name: X-Request-Id\n" + " required: true\n" + " schema:\n" + " type: integer\n" + " example: 37\n" + " responses:\n" + " '200':\n" + " description: A response with the specified headers\n" + " headers:\n" + " Server:\n" + " schema:\n" + " type: string\n" + " examples:\n" + " httpbin:\n" + " value: httpbin\n" + " unicorn:\n" + " value: unicorn\n" + " X-Request-Id:\n" + " schema:\n" + " type: integer\n" + " example: 37\n" + "\n" + "components:\n" + " schemas:\n" + " AnyValue:\n" + " nullable: true\n" + " description: Can be anything - string, object, array, null, etc.\n" + "\n" + " examples:\n" + " AString:\n" + " value: Hi there\n" + " ANumber:\n" + " value: 42\n" + " ANull:\n" + " value: null\n" + " AnArray:\n" + " value: [1, 2, 3]\n" + " AnObject:\n" + " value:\n" + " id: 42\n" + " name: Arthur Dent";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readContents(yaml, auths, options);
OpenAPI openAPI = result.getOpenAPI();
MediaType mediaTypeJson = openAPI.getPaths().get("/post").getPost().getRequestBody().getContent().get("application/json");
Header header1 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("Server");
Header header2 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("X-Request-Id");
Parameter parameter1 = openAPI.getPaths().get("/").getGet().getParameters().get(0);
Parameter parameter2 = openAPI.getPaths().get("/").getGet().getParameters().get(1);
Assert.assertNotNull(mediaTypeJson.getExamples());
Assert.assertEquals(mediaTypeJson.getExamples().get("AnObject").get$ref(), "#/components/examples/AnObject");
Assert.assertNotNull(header1.getExamples());
Assert.assertEquals(header1.getExamples().get("httpbin").getValue(), "httpbin");
Assert.assertNotNull(header2.getExample());
Assert.assertEquals(header2.getExample(), 37);
Assert.assertNotNull(parameter1.getExamples());
Assert.assertEquals(parameter1.getExamples().get("unicorn").getValue(), "unicorn");
Assert.assertNotNull(parameter2.getExample());
Assert.assertEquals(parameter2.getExample(), 37);
}
use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.
the class RemoteUrlTest method testAuthorizationHeaderWithMatchingUrl.
@Test
public void testAuthorizationHeaderWithMatchingUrl() throws Exception {
final String expectedBody = setupStub();
final String headerName = "Authorization";
final String headerValue = "foobar";
final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header", url -> url.toString().startsWith("http://localhost"));
final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
assertEquals(actualBody, expectedBody);
verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withHeader(headerName, equalTo(headerValue)));
}
use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.
the class OpenAPIParserTest method testIssue844.
@Test
public void testIssue844() {
OpenAPIParser openApiParser = new OpenAPIParser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
OpenAPI openAPI = openApiParser.readLocation("reusableParametersWithExternalRef.json", null, options).getOpenAPI();
assertNotNull(openAPI);
assertEquals(openAPI.getPaths().get("/pets/{id}").getGet().getParameters().get(0).getIn(), "header");
}
Aggregations