简体   繁体   English

如何在 Spring Cloud Contract Groovy DSL Contract 定义中将请求正文对象属性定义为可为空?

[英]How define request body object property as nullable in Spring Cloud Contract Groovy DSL Contract definition?

I'm working on Spring Cloud based microservices, with spring cloud contract validation of client/API between services.我正在研究基于 Spring Cloud 的微服务,对服务之间的客户端/API 进行 Spring Cloud 合同验证。 WireMock is used in client service side tests for services API stubbing. WireMock 用于服务 API 存根的客户端服务端测试。 Spring Cloud is in version 2020.0.2 . Spring Cloud 的版本为2020.0.2 I have following Groovy contract definition:我有以下 Groovy 合同定义:

Contract.make {
description "Nullable sender & receiver API contract"

request {
    method POST()
    url value(consumer(regex('/message')))
    headers {
        contentType(applicationJson())
        header("x-channelId", anyNonBlankString())
    }
    body(
            sender: [
                    toEmail            : anyEmail(),
                    firstName          : anyNonBlankString(),
                    lastName           : anyNonBlankString()
            ],
            receiver: [
                    toEmail            : anyEmail(),
                    firstName          : anyNonBlankString(),
                    lastName           : anyNonBlankString()
            ]
    )
}
response {
    status CREATED()
}
}

The think I want to achieve is to have both 'sender', and 'receiver' nullable.我想要实现的想法是让“发送者”和“接收者”都可以为空。 I can define nullable (DSL optional() ) each sub-field of both, but request body can look like this:我可以为两者的每个子字段定义可为空的(DSL optional() ),但请求正文可能如下所示:

{sender:null, receiver:null}

I've tried define multiple contracts for each possibility(one with both filled, second with sender: null , third with receiver: null , and both null).我已经尝试为每种可能性定义多个合同(一个同时填充,第二个是sender: null ,第三个是receiver: null ,两个都为 null)。 In this case, client service side WireMock, that uses above definitions for API stubbing, does not match wanted stubs to proper requests - eg when calling using request with empty sender , WireMock identifies as closest stub one with expected sender and returns 404, ignoring contract definition that expects empty sender .在这种情况下,客户服务端WireMock,上面API存根定义使用,不希望存根匹配到合适的请求-例如使用要求与空发送者打电话时,WireMock标识为最接近的存根一个与期望的发送者,并返回404,无视合同需要空的sender 的定义。

Is there way to define field with internal structure as nullable?有没有办法将具有内部结构的字段定义为可以为空? In documentation I've only saw possibility to make nullable field value, but not whole structure.在文档中,我只看到了创建可为空字段值的可能性,而不是整个结构。

If a field is potentially null you should create 2 contracts.如果一个字段可能为null您应该创建 2 个合同。 1 with it being null and 1 with it not being null . 1 为null ,1 为非null

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

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