Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io.restassured.builder.ResponseBuilder.build() ignores io.restassured.RestAssured.registerParser(String contentType, Parser parser) #1759

Open
theoamonteiro opened this issue Dec 27, 2023 · 0 comments

Comments

@theoamonteiro
Copy link

I think 51443bc should actually check for NPE before creating a new instance of ResponseParserRegistrar. I was trying to parse a application/yaml by filtering the response and converting from YAML to JSON using Jackson:

class YamlToJsonFilter implements OrderedFilter {

    @Override
    public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
        try {
            Response response = ctx.next(requestSpec, responseSpec);
            
            if(!"application/yaml".equals(response.contentType())) {
            	return response;
            }

            ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
            Object obj = yamlReader.readValue(response.getBody().asString(), Object.class);

            ObjectMapper jsonWriter = new ObjectMapper();
            String json = jsonWriter.writeValueAsString(obj);

            ResponseBuilder builder = new ResponseBuilder();
            builder.clone(response);
            builder.setBody(json);
            
            Response result = builder.build();
            if(result instanceof RestAssuredResponseImpl tweakedResult) {
            	tweakedResult.getRpr().registerParser("application/yaml", Parser.JSON);
            }
            return result;
        } catch (Exception e) {
            throw new IllegalStateException("Failed to convert the request: " + ExceptionUtils.getMessage(e), e);
        }
    }

    @Override
    public int getOrder() {
        return OrderedFilter.HIGHEST_PRECEDENCE;
    }

}

I had to make the tweak because I noticed that RestAssured.registerParser("application/yaml", Parser.JSON) had no effect. Maybe I am missing something and there is a better way to parse YAML and validate the properties with io.restassured.response.ValidatableResponseOptions.body(String path, ResponseAwareMatcher<Response> responseAwareMatcher). Please show me a better way if it is the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant