io.swagger.models.RefResponse Java Examples

The following examples show how to use io.swagger.models.RefResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a json string using the swagger object.
 *
 * @param swaggerObj swagger object
 * @return json string using the swagger object
 * @throws APIManagementException error while creating swagger json
 */
private String getSwaggerJsonString(Swagger swaggerObj) throws APIManagementException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    //this is to ignore "originalRef" in schema objects
    mapper.addMixIn(RefModel.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefProperty.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefPath.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefParameter.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefResponse.class, IgnoreOriginalRefMixin.class);

    //this is to ignore "responseSchema" in response schema objects
    mapper.addMixIn(Response.class, ResponseSchemaMixin.class);
    try {
        return new String(mapper.writeValueAsBytes(swaggerObj));
    } catch (JsonProcessingException e) {
        throw new APIManagementException("Error while generating Swagger json from model", e);
    }
}
 
Example #2
Source File: OASParserUtil.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a json string using the swagger object.
 *
 * @param swaggerObj swagger object
 * @return json string using the swagger object
 * @throws APIManagementException error while creating swagger json
 */
public static String getSwaggerJsonString(Swagger swaggerObj) throws APIManagementException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    //this is to ignore "originalRef" in schema objects
    mapper.addMixIn(RefModel.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefProperty.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefPath.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefParameter.class, IgnoreOriginalRefMixin.class);
    mapper.addMixIn(RefResponse.class, IgnoreOriginalRefMixin.class);

    //this is to ignore "responseSchema" in response schema objects
    mapper.addMixIn(Response.class, ResponseSchemaMixin.class);
    try {
        return new String(mapper.writeValueAsBytes(swaggerObj));
    } catch (JsonProcessingException e) {
        throw new APIManagementException("Error while generating Swagger json from model", e);
    }
}
 
Example #3
Source File: ResponseDeserializer.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override public Response deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    JsonNode sub = node.get("$ref");

    if (sub != null) {
        return Json.mapper().convertValue(node, RefResponse.class);
    } else {
        Response response = Json.responseMapper().convertValue(node, Response.class);
        return response;
    }
}
 
Example #4
Source File: SwaggerExtension.java    From Web-API with MIT License 4 votes vote down vote up
@Override
public void decorateOperation(Operation operation, Method method,
                              Iterator<io.swagger.jaxrs.ext.SwaggerExtension> chain) {
    // Automatically add query params
    operation.addParameter(new RefParameter("details"));
    operation.addParameter(new RefParameter("accept"));
    operation.addParameter(new RefParameter("pretty"));

    // Automatically add 500 as a possible response
    operation.addResponse("500", new RefResponse("500"));

    // Automatically add error codes depending on thrown exceptions
    for (Class<?> execClass : method.getExceptionTypes()) {
        if (BadRequestException.class.isAssignableFrom(execClass)) {
            operation.addResponse("400", new RefResponse("400"));
        }
        if (NotFoundException.class.isAssignableFrom(execClass)) {
            operation.addResponse("404", new RefResponse("404"));
        }
        if (NotImplementedException.class.isAssignableFrom(execClass)) {
            operation.addResponse("501", new RefResponse("501"));
        }
    }

    Permission[] perms = method.getAnnotationsByType(Permission.class);
    if (perms.length > 0) {
        // Automatically add 401 & 403 as a possible response
        operation.addResponse("401", new RefResponse("401"));
        operation.addResponse("403", new RefResponse("403"));

        // Automatically add required permission notes if we have a @Permission annotation
        Path path = method.getDeclaringClass().getAnnotation(Path.class);
        String prefix = path != null ? path.value() + "." : "";

        StringBuilder permStr = new StringBuilder("  \n\n **Required permissions:**  \n\n");
        for (Permission perm : perms) {
            permStr.append("- **").append(prefix).append(String.join(".", perm.value())).append("**  \n");
        }

        operation.setDescription(operation.getDescription() + permStr.toString());

        // Add security definitions
        operation.addSecurity("ApiKeyHeader", new ArrayList<>());
        operation.addSecurity("ApiKeyQuery", new ArrayList<>());
    }
    super.decorateOperation(operation, method, chain);
}
 
Example #5
Source File: SwaggerConverter.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public ApiResponse convert(io.swagger.models.Response v2Response, List<String> produces) {
    ApiResponse response = new ApiResponse();
    Content content = new Content();

    if (v2Response instanceof RefResponse) {

        RefResponse ref = (RefResponse) v2Response;
        if (ref.get$ref().indexOf("#/responses") == 0) {
            String updatedRef = "#/components/responses" + ref.get$ref().substring("#/responses".length());
            ref.set$ref(updatedRef);
        }

        response.set$ref(ref.get$ref());
    } else {

        List<String> mediaTypes = new ArrayList<>(globalProduces);
        if (produces != null) {
            // use this for media type
            mediaTypes.clear();
            mediaTypes.addAll(produces);
        }

        if (mediaTypes.size() == 0) {
            mediaTypes.add("*/*");
        }

        response.setDescription(v2Response.getDescription());

        if (v2Response.getSchema() != null) {
            Schema schema = convertFileSchema(convert(v2Response.getSchema()));
            for (String type : mediaTypes) {
                // TODO: examples
                MediaType mediaType = new MediaType();
                content.addMediaType(type, mediaType.schema(schema));
            }
            response.content(content);
        }

        response.content(convertExamples(v2Response.getExamples(), content));
        response.setExtensions(convert(v2Response.getVendorExtensions()));

        if (v2Response.getHeaders() != null && v2Response.getHeaders().size() > 0) {
            response.setHeaders(convertHeaders(v2Response.getHeaders()));
        }
    }

    return response;
}