Search in sources :

Example 31 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project cas by apereo.

the class RegisteredServicesEndpoint method importSingleService.

private ResponseEntity<RegisteredService> importSingleService(final HttpServletRequest request) throws IOException {
    val requestBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
    LOGGER.trace("Submitted registered service:\n[{}]", requestBody);
    if (StringUtils.isBlank(requestBody)) {
        LOGGER.warn("Could not extract registered services from request body");
        return ResponseEntity.badRequest().build();
    }
    return registeredServiceSerializers.stream().map(serializer -> serializer.from(requestBody)).filter(Objects::nonNull).findFirst().map(service -> {
        LOGGER.trace("Storing registered service:\n[{}]", service);
        return servicesManager.save(service);
    }).map(service -> {
        val headers = new HttpHeaders();
        headers.put("id", CollectionUtils.wrapList(String.valueOf(service.getId())));
        return new ResponseEntity<>(service, headers, HttpStatus.CREATED);
    }).orElseGet(() -> new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) PathVariable(org.springframework.web.bind.annotation.PathVariable) ActuatorMediaType(org.springframework.boot.actuate.endpoint.http.ActuatorMediaType) ZipInputStream(java.util.zip.ZipInputStream) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) StringUtils(org.apache.commons.lang3.StringUtils) WebApplicationService(org.apereo.cas.authentication.principal.WebApplicationService) Operation(io.swagger.v3.oas.annotations.Operation) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentDisposition(org.springframework.http.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) StringSerializer(org.apereo.cas.util.serialization.StringSerializer) CollectionUtils(org.apereo.cas.util.CollectionUtils) GetMapping(org.springframework.web.bind.annotation.GetMapping) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ServiceFactory(org.apereo.cas.authentication.principal.ServiceFactory) ServicesManager(org.apereo.cas.services.ServicesManager) Resource(org.springframework.core.io.Resource) PostMapping(org.springframework.web.bind.annotation.PostMapping) Unchecked(org.jooq.lambda.Unchecked) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) CompressionUtils(org.apereo.cas.util.CompressionUtils) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RegisteredService(org.apereo.cas.services.RegisteredService) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) BaseCasActuatorEndpoint(org.apereo.cas.web.BaseCasActuatorEndpoint) RestControllerEndpoint(org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) HttpStatus(org.springframework.http.HttpStatus) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) JacksonObjectMapperFactory(org.apereo.cas.util.serialization.JacksonObjectMapperFactory) NumberUtils(org.apache.commons.lang3.math.NumberUtils) ResponseEntity(org.springframework.http.ResponseEntity) HttpHeaders(org.springframework.http.HttpHeaders) Objects(java.util.Objects)

Example 32 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project cas by apereo.

the class YubiKeyAccountRegistryEndpoint method importAccount.

/**
 * Import account.
 *
 * @param request the request
 * @return the http status
 * @throws Exception the exception
 */
@Operation(summary = "Import a Yubikey account as a JSON document")
@PostMapping(path = "/import", consumes = MediaType.APPLICATION_JSON_VALUE)
public HttpStatus importAccount(final HttpServletRequest request) throws Exception {
    val requestBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
    LOGGER.trace("Submitted account: [{}]", requestBody);
    val account = MAPPER.readValue(requestBody, new TypeReference<YubiKeyAccount>() {
    });
    LOGGER.trace("Storing account: [{}]", account);
    registry.getObject().save(account);
    return HttpStatus.CREATED;
}
Also used : lombok.val(lombok.val) YubiKeyAccount(org.apereo.cas.adaptors.yubikey.YubiKeyAccount) PostMapping(org.springframework.web.bind.annotation.PostMapping) Operation(io.swagger.v3.oas.annotations.Operation)

Example 33 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project cas by apereo.

the class WebAuthnRegisteredDevicesEndpoint method importAccount.

/**
 * Import account.
 *
 * @param request the request
 * @return the http status
 * @throws Exception the exception
 */
@Operation(summary = "Import a device registration as a JSON document")
@PostMapping(path = "/import", consumes = MediaType.APPLICATION_JSON_VALUE)
public HttpStatus importAccount(final HttpServletRequest request) throws Exception {
    val requestBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
    LOGGER.trace("Submitted account: [{}]", requestBody);
    val account = WebAuthnUtils.getObjectMapper().readValue(requestBody, new TypeReference<CredentialRegistration>() {
    });
    LOGGER.trace("Storing account: [{}]", account);
    registrationStorage.getObject().addRegistrationByUsername(account.getUsername(), account);
    return HttpStatus.CREATED;
}
Also used : lombok.val(lombok.val) CredentialRegistration(com.yubico.data.CredentialRegistration) PostMapping(org.springframework.web.bind.annotation.PostMapping) Operation(io.swagger.v3.oas.annotations.Operation)

Example 34 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-core by swagger-api.

the class ParameterDeSerializationTest method deserializeArrayBodyParameter.

@Test(description = "it should deserialize an array BodyParameter")
public void deserializeArrayBodyParameter() throws IOException {
    final String json = "{\"content\":{\"*/*\":{\"schema\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Cat\"}}}}}";
    final RequestBody p = m.readValue(json, RequestBody.class);
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 35 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-core by swagger-api.

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.addExtension("x-test2", map);
    info.addExtension("x-test", "value");
    final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id").deprecated(true);
    get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
    get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
    final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Person")).example("fun")));
    final ApiResponse errorResponse = new ApiResponse().description("error response").addLink("myLink", new Link().description("a link").operationId("theLinkedOperationId").addParameter("userId", "gah")).content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse));
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").addTagsItem("Pet Operations").operationId("add pet").responses(new ApiResponses().addApiResponse("default", errorResponse)).requestBody(new RequestBody().description("the pet to add").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person")))));
    swagger.paths(new Paths().addPathItem("/pets", new PathItem().get(get).post(post)));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    Json.prettyPrint(swagger);
    final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) HashMap(java.util.HashMap) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Contact(io.swagger.v3.oas.models.info.Contact) PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)67 OpenAPI (io.swagger.v3.oas.models.OpenAPI)62 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)59 Schema (io.swagger.v3.oas.models.media.Schema)46 Operation (io.swagger.v3.oas.annotations.Operation)41 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)40 MediaType (io.swagger.v3.oas.models.media.MediaType)36 StringSchema (io.swagger.v3.oas.models.media.StringSchema)35 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)32 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)32 Content (io.swagger.v3.oas.models.media.Content)31 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)28 Operation (io.swagger.v3.oas.models.Operation)27 PathItem (io.swagger.v3.oas.models.PathItem)23 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)21 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)14 Components (io.swagger.v3.oas.models.Components)13 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)12