简体   繁体   English

RestAssured 不尊重 Quarkus 中的 ObjectMapper 配置

[英]RestAssured does not respect ObjectMapper configuration in Quarkus

I've made a very simple adjustment to the ObjectMapper configuration in my Quarkus application, as described per the Quarkus guides:如 Quarkus 指南所述,我对我的 Quarkus 应用程序中的ObjectMapper配置进行了非常简单的调整:

@Singleton
public class ObjectMapperConfig implements ObjectMapperCustomizer {

    @Override
    public void customize(ObjectMapper objectMapper) {
        objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
        objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
        objectMapper.registerModule(new JavaTimeModule());
    }

}

I've done this to wrap/unwrap my objects with the @JsonRootName annotation:我这样做是为了使用@JsonRootName注释来包装/解包我的对象:

@RegisterForReflection
@JsonRootName("article")
public class CreateArticleRequest {

    private CreateArticleRequest(String title, String description, String body, List<String> tagList) {
        this.title = title;
        this.description = description;
        this.body = body;
        this.tagList = tagList;
    }

    private String title;
    private String description;
    private String body;
    private List<String> tagList;

    ... 

}

This works just fine when curl against my actual API, but whenever I use RestAssured in one of my tests, RestAssured does not seem to respect my ObjectMapper config, and does not wrap the CreateArticleRequest as it should be doing as indicated by the @JsonRootName annotation.curl与我的实际 API 对比时,这工作得很好,但是每当我在我的一个测试中使用 RestAssured 时,RestAssured 似乎不尊重我的 ObjectMapper 配置,并且不包装 CreateArticleRequest,因为它应该按照@JsonRootName注释指示的那样做.

@QuarkusTest
public class ArticleResourceTest {

    @Test
    public void testCreateArticle() {
        given()
            .when()
            .body(CREATE_ARTICLE_REQUEST)
            .contentType(ContentType.JSON)
            .log().all()
            .post("/api/articles")
            .then()
            .statusCode(201)
            .body("", equalTo(""))
            .body("article.title", equalTo(ARTICLE_TITLE))
            .body("article.favorited", equalTo(ARTICLE_FAVORITE))
            .body("article.body", equalTo(ARTICLE_BODY))
            .body("article.favoritesCount", equalTo(ARTICLE_FAVORITE_COUNT))
            .body("article.taglist", equalTo(ARTICLE_TAG_LIST));
    }

}

This serializes my request body as:这将我的请求正文序列化为:

{
    "title": "How to train your dragon",
    "description": "Ever wonder how?",
    "body": "Very carefully.",
    "tagList": [
        "dragons",
        "training"
    ]
}

... instead of... ... 代替...

{
    "article": {
        "title": "How to train your dragon",
        "description": "Ever wonder how?",
        "body": "Very carefully.",
        "tagList": [
            "dragons",
            "training"
        ]
    }
}

I can actually fix this, by manually configuring the RestAssured ObjectMapper, like so:我实际上可以通过手动配置 RestAssured ObjectMapper 来解决这个问题,如下所示:

@QuarkusTest
public class ArticleResourceTest {

    @BeforeEach
    void setUp() {
        RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
            (cls, charset) -> {
                ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
                mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
                mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
                mapper.registerModule(new JavaTimeModule());
                return mapper;
            }
        ));
    }
}

However, I obviously do not want to do this.但是,我显然不想这样做。 I wanted RestAssured to pick up my ObjectMapper config so that I don't need to keep around two different ObjectMapper configurations.我希望 RestAssured 拿起我的 ObjectMapper 配置,这样我就不需要保留两个不同的 ObjectMapper 配置。

Why is it not being picked up?为什么不被捡起来? What am I missing?我错过了什么?

So actually Quarkus won't be doing this automatically (due to the fact that it would interfere with the native tests).所以实际上 Quarkus 不会自动执行此操作(因为它会干扰本机测试)。

You can however use:但是,您可以使用:

@Inject
ObjectMapper

inside the test to setup RestAssured.config在测试中设置RestAssured.config

Just to update in case someone find this like me...只是为了更新以防有人发现像我这样......

I just add the Singleton ObjectMapperConfig and this is working on我只是添加了 Singleton ObjectMapperConfig 这正在处理

<quarkus.platform.version>2.3.0.Final</quarkus.platform.version>

    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>

I solved this with我解决了这个问题

@QuarkusTest
public class RESTResourceTest {

    @Inject
    ObjectMapper mapper;

    @BeforeEach
    void setUp() {
        RestAssured.config = RestAssured.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory( (cls, charset) -> mapper ));
    }

where somewhere else in my src/main/java I have my custom ObjectMapper customizer:在我的src/main/java的其他地方我有我的自定义 ObjectMapper 定制器:

import javax.inject.Singleton;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.cloudevents.jackson.JsonFormat;
import io.quarkus.jackson.ObjectMapperCustomizer;

@Singleton
public class QuarkusObjectMapperCustomizer implements ObjectMapperCustomizer {

    public void customize(ObjectMapper mapper) {
        mapper.registerModule(JsonFormat.getCloudEventJacksonModule());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM