无法使用 json 架构验证器根据预定义的 yaml 文件验证查询参数

2024-05-18

我需要根据预定义的 yaml 文件架构验证查询参数的架构,因此我使用 json 架构验证器。验证如何失败。

我正在执行以下步骤:

  1. 填充参数和相应的架构。

    final List<Parameter> parameters = openAPI.getPaths().get(requestPath).getGet().getParameters()
                            .stream().filter(parameter -> Objects.nonNull(parameter.getIn()) && parameter.getIn().equalsIgnoreCase("query"))
                            .collect(Collectors.toList());
    
                    final Map<Parameter, JsonNode> parameterAndSchema = parameters.stream().collect(Collectors.toMap(Function.identity(), parameter -> {
                        JsonNode parameterSchema;
                        try {
                            final Schema schema = parameter.getSchema();
                            parameterSchema = mapper.readTree(objectWriter.writeValueAsString(schema));
    
                            return parameterSchema;
                        } catch (JsonProcessingException e) {
                            throw new RuntimeException(e);
                        }
                    }));
    
  2. 创建 queryParameterSchema 以根据步骤 1 中准备的相应架构验证查询参数:

    用于测试的硬编码查询参数

     final Map<String,String> queryParameterMap = Map.of("test-parameter", "testValue1");
        JsonNode queryParameterSchema = new ObjectMapper()
                                                   .readTree(queryParameterMap,JsonNode.class)
    
  3. 将步骤 1 的 schema(从 yaml 准备)转换为 JsonSchema,如下所示:

    JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
    
    SchemaValidatorsConfig config = new SchemaValidatorsConfig();
    config.setTypeLoose(true);
    config.setFailFast(false);
    JsonSchema jsonSchema = schemaFactory.getSchema(schema, config);
    processingReport = jsonSchema.validate(queryParameterSchema , queryParameterSchema , at);
    
    Sample yaml file:
      paths:
        /test-instances:
          get:
            tags:
            - Instances (Store)
            summary: Test summary
            operationId: SearchInstances
            parameters:
            - name: test-parameter
              in: query
              description: Names of the services offered
              required: false
              style: form
              explode: false
              schema:
                type: string
    

但是,当我尝试针对此 JsonSchema 验证 queryParameterSchema 时,会调用 TypeValidator 并始终返回 false,因为在第 2 步填充的 queryParameterSchema 始终作为模式类型为 OBJECT 的对象节点,而验证器模式类型作为 String (因为它在中定义为 String) yaml),

我想我可能必须在步骤 2 中以不同的方式创建查询参数架构,但不确定如何


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用 json 架构验证器根据预定义的 yaml 文件验证查询参数 的相关文章

随机推荐