【问题标题】:How to document a Map with Enum as key entry?如何用枚举作为键条目记录地图?
【发布时间】:2021-12-15 18:15:48
【问题描述】:

我正在为返回这种 JSON 的 REST 服务做文档:

{
  "CKM_RSA_PKCS_OAEP" : {
    "keyType" : "RSA",
    "canGenerateKey" : false,
    "canEncryptDecrypt" : true,
    "canSignVerify" : false,
    "canWrapSecretKeys" : true,
    "canWrapPrivateKeys" : false,
    "canDerive" : false,
    "canDigest" : false,
    "minKeySize" : 512,
    "maxKeySize" : 16384
  },
  "CKM_SHA384" : {
    "canGenerateKey" : false,
    "canEncryptDecrypt" : false,
    "canSignVerify" : false,
    "canWrapSecretKeys" : false,
    "canWrapPrivateKeys" : false,
    "canDerive" : false,
    "canDigest" : true,
    "minKeySize" : 0,
    "maxKeySize" : 0
  }
}

这表示Map<Mechanism, MechanismInfo>,其中Mechanism 是枚举,MechanismInfo 是 POJO。

我想只记录MechanismInfo 类的字段,而不管Mechanism 的约束条件是keyType 属性可以是null,然后不存在于响应中。所有其他属性都是整数或布尔值,并且具有默认值。

这是我的 Junit 5 测试的一部分:

this.webTestClient = WebTestClient.bindToApplicationContext(applicationContext)
  .configureClient()
  .filter(documentationConfiguration(restDocumentation)
    .operationPreprocessors()
    .withResponseDefaults(prettyPrint()))
  .build();
(...)
webTestClient
  .get().uri("/tokens/{id}/mechanisms", TOKEN_TEST)
  .exchange()
  .expectStatus().isOk()
  .expectBody(new ParameterizedTypeReference<Map<Mechanism, MechanismInfo>>() {
  })
  .consumeWith(document(
   "get_mechanisms"
  ));

有人可以帮我做这件事吗?

【问题讨论】:

    标签: enums spring-restdocs


    【解决方案1】:

    当我终于找到解决这个问题的方法时,我发布了我的做法。 要点是在其文档之前修改请求的内容,如Spring REST Docs documentation 中所述。
    所以,我创建了自己的OperationPreprocessor 来修改响应,利用内置的ContentModifyingOperationPreprocessor,它只需要实现ContentModifier 接口。其想法是能够提取我知道它始终存在的独特价值。

    这是它的样子:

    private final class MechanismsExtract implements ContentModifier {
      @Override
      public byte[] modifyContent(byte[] originalContent, MediaType contentType) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
          Map<String, LinkedHashMap> map = objectMapper.readValue(originalContent, Map.class);
          final LinkedHashMap mechanismInfo = map.entrySet().stream()
            .filter(
              key -> key.getKey().equals(Mechanism.CKM_RSA_PKCS_KEY_PAIR_GEN.name())
            )
            .map(
              Map.Entry::getValue
            )
            .findFirst()
            .orElseThrow();
    
          return objectMapper.writeValueAsBytes(mechanismInfo);
        } catch (IOException ex) {
          return originalContent;
        }
      }
    }
    

    然后在记录时:

    (...)
      .consumeWith(document(
        "get_mechanisms",
        // Output is limited to chosen mechanisms for documentation readability reasons
        Preprocessors.preprocessResponse(
          new ContentModifyingOperationPreprocessor(
            new MechanismsExtract()
          )
        ),
        relaxedResponseFields(
          fieldWithPath(NAME).description(DESCRIPTION),
          (...)
        )
        (...)
      )
    (...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 2021-06-29
      • 2014-04-10
      • 2020-12-05
      相关资源
      最近更新 更多