簡體   English   中英

RestAssured 不尊重 Quarkus 中的 ObjectMapper 配置

[英]RestAssured does not respect ObjectMapper configuration in Quarkus

如 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());
    }

}

我這樣做是為了使用@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;

    ... 

}

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));
    }

}

這將我的請求正文序列化為:

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

... 代替...

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

我實際上可以通過手動配置 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;
            }
        ));
    }
}

但是,我顯然不想這樣做。 我希望 RestAssured 拿起我的 ObjectMapper 配置,這樣我就不需要保留兩個不同的 ObjectMapper 配置。

為什么不被撿起來? 我錯過了什么?

所以實際上 Quarkus 不會自動執行此操作(因為它會干擾本機測試)。

但是,您可以使用:

@Inject
ObjectMapper

在測試中設置RestAssured.config

只是為了更新以防有人發現像我這樣......

我只是添加了 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>

我解決了這個問題

@QuarkusTest
public class RESTResourceTest {

    @Inject
    ObjectMapper mapper;

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

在我的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