【问题标题】:How to specify an optional Date property in spring cloud contract?如何在 Spring Cloud 合约中指定可选的 Date 属性?
【发布时间】:2018-07-13 11:48:38
【问题描述】:

我正在尝试使用 Spring Cloud 合约。我编写了一个 JSON 正文规范,我需要一个 Date 属性是可选的。 当我在 groovy dsl 中指定时:

"processingDate": $(consumer(optional(anyDate())), producer(anyDate())),

失败了:

Exception in thread "main" Assertion failed: 

assert testSide ==~ Pattern.compile(stubSide.optionalPattern())
       |        |           |       |        |
       |        false       |       |        (org.springframework.cloud.contract.spec.internal.ClientDslProperty(DslProperty(clientValue:(\d\d\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]), serverValue:2016-06-16)))?
       |                    |       (org.springframework.cloud.contract.spec.internal.ClientDslProperty(DslProperty(clientValue:(\d\d\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]), serverValue:2016-06-16)))?
       |                    (org.springframework.cloud.contract.spec.internal.ClientDslProperty(DslProperty(clientValue:(\d\d\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]), serverValue:2016-06-16)))?
       org.springframework.cloud.contract.spec.internal.ClientDslProperty(DslProperty(clientValue:(\d\d\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]), serverValue:2011-01-11))

然而,当使用 optional 和正则表达式时它可以工作:

"processingDate": $(consumer(optional(regex(isoDate()))), producer(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE))),

但显然我不想为每个可选字段使用这样的解决方法。

谢谢!

【问题讨论】:

  • 你能在这里分享你的groovy合同文件吗?
  • 上面的第一个代码示例是我的 groovy 合约中的相关行。

标签: spring-cloud spring-cloud-contract


【解决方案1】:

似乎在consumer 边添加regex 前缀就可以了。尝试改变

"processingDate": $(consumer(optional(anyDate())), producer(anyDate())),

"processingDate": $(consumer(optional(regex(anyDate()))), producer(anyDate())),

"processingDate": $(consumer(optional(regex(isoDate()))), producer('2016-06-16')),

详情请查看spring-cloud-contract_optional_parameters

【讨论】:

    【解决方案2】:

    anyDate()应该这样直接使用:

    "processingDate": ${anyDate()}
    

    如果您需要正则表达式,请使用isoDate()。使用org.springframework.cloud.contract.spec.internal.PatternValueDslPropertyorg.springframework.cloud.contract.spec.internal.RegexPatterns的代表是有区别的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2017-05-03
      • 2017-07-23
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      相关资源
      最近更新 更多