[英]Cannot remove specific key: value from YAML file
目標:我想從 YAML 文件中刪除特定鍵及其關聯值。
預期結果:應該從 YAML 文件中刪除orginalRef
鍵和它的值,並且應該生成一個新的 YAML 文件。
實際結果: responses
屬性下的原始orginalRef
值未刪除。
錯誤消息:沒有錯誤消息。
我嘗試過的:我有一個 swagger YAML 文件,我只是將它加載到 Jackson ObjectMapper 並嘗試從該 YAML 文件中刪除一些鍵。 然后使用我所做的更改生成一個新的 YAML 文件。
代碼:下面是我巨大的 YAML 文件的一部分。 它的名字是api.yaml
---
swagger: "2.0"
info:
description: "Planner application is a proposal system enables user to create, manage\
\ plan . \nIt also enables network user to publish plan "
version: "1.0"
title: "Planner API"
contact:
name: "API team"
email: "apiteam@test.com"
license:
name: "Copyright © 2020 test
host: "aos-dev.test.com"
basePath: "/unifiedplanner"
tags:
- name: "Additional Fee APIs"
description: "Additional Fee Controller"
- name: "Condition APIs"
description: "Condition Controller"
paths:
/v1/{apiKey}/entity/{entityId}:
post:
tags:
- "Condition APIs"
summary: "Save New Conditions Entity"
operationId: "saveNewConditions"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: "Authorization"
in: "header"
description: "Authorization"
required: true
type: "string"
- name: "apiKey"
in: "path"
description: "API Key"
required: true
type: "string"
- in: "body"
name: "conditions"
description: "Conditions Entity that needs to be saved"
required: true
schema:
$ref: "#/definitions/Conditions"
- name: "entityId"
in: "path"
description: "Entity ID"
required: true
type: "string"
responses:
"200":
description: "OK"
schema:
$ref: "#/definitions/Conditions"
originalRef: "Conditions"
deprecated: false
put:
tags:
- "Condition APIs"
summary: "Modify / Overwrite existing Conditions Entity"
operationId: "modifyConditions"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: "Authorization"
in: "header"
description: "Authorization"
required: true
type: "string"
- name: "apiKey"
in: "path"
description: "API Key"
required: true
type: "string"
- in: "body"
name: "conditions"
description: "Conditions Entity that needs to be updated"
required: true
schema:
$ref: "#/definitions/Conditions"
- name: "entityId"
in: "path"
description: "Entity ID"
required: true
type: "string"
responses:
"200":
description: "OK"
schema:
$ref: "#/definitions/Conditions"
originalRef: "Conditions"
deprecated: false
definitions:
AbstractCondition:
type: "object"
properties:
externalTrafficSystem:
$ref: "#/definitions/ExternalTrafficSystem"
originalRef: "ExternalTrafficSystem"
id:
type: "string"
version:
type: "integer"
format: "int64"
title: "AbstractCondition"
如您所見,我只想從上述 YAML 文件中的responses
屬性中刪除originalRef
鍵及其值。
下面是我的 YamlProcessor.java 類,它完成所有必要的事情。
package com.aos.tools.aostool.yaml;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Stream;
@Service
public class YamlProcessor {
public YamlProcessor() throws IOException {
process();
}
public void process() throws IOException {
final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
final Map<String, Object> api = objectMapper.readValue(new File("api.yaml"),
new TypeReference<Map<String, Object>>() {
});
final Map<String, Object> path = (Map<String, Object>) api.get("paths");
processPath(path);
// write YAML file
final Date date = Calendar.getInstance().getTime();
objectMapper.writeValue(new File(date.toString() + "api-updated.yaml"), api);
}
private void processPath(final Map<String, Object> paths) {
paths.entrySet().forEach(path -> {
if (null != path) {
final Map<String, Object> level1Child = (Map<String, Object>) path.getValue();
if (null != level1Child) {
level1Child.entrySet().forEach(method -> {
final Map<String, Object> methodValues = (Map<String, Object>) method.getValue();
methodValues.entrySet().forEach(methodValue -> {
if (null != methodValue && methodValue.getKey().equals("parameters")) {
final List<Object> paramValue = (List<Object>) methodValue.getValue();
paramValue.forEach(paramValueChild -> {
if (null != paramValueChild) {
final Map<String, Object> paramMap = (HashMap<String, Object>) paramValueChild;
paramMap.entrySet().forEach(k -> {
if (k.getKey().contains("schema")) {
final Map<String, Object> schema = (HashMap<String, Object>) k.getValue();
// this line works fine , this will remove all the originalRef keys under parameters property
schema.remove("originalRef");
}
});
}
});
}
// this is where I'm currently getting stucked.
if (null != methodValue && methodValue.getKey().equals("responses")) {
Map<String,Object> value = (HashMap<String,Object>) methodValue.getValue();
if(value.containsKey("200")) {
value.entrySet().forEach(k -> {
if(k.getKey().contains("schema")) {
Map<String,Object> responseSchema = (HashMap<String,Object>)k.getValue();
responseSchema.remove("originalRef");
}
});
}
}
});
});
}
}
});
}
}
當我運行應用程序時,它運行良好,沒有任何錯誤。 同時生成新的 YAML 文件。 但是在新的 YAML 文件中,響應屬性下仍然包含 originlaRef 鍵。 任何人都可以指導我在這里做錯了什么。 任何幫助將不勝感激。 干杯! 再會!
您正在迭代 value 的內容,而不是 value.200
if(value.containsKey("200")) {
value.entrySet().forEach(k -> {
應該是
if(value.containsKey("200")) {
value.get("200").entrySet().forEach(k -> {
在任何情況下,您都可以將日志語句放在循環中以調試此類事情。
最后,我能夠通過試驗我的代碼獲得所需的輸出。
以下是我的工作解決方案。
if(value.containsKey("200")) {
value.entrySet().forEach(k -> {
Map<String,Object> responseSchema = (HashMap<String,Object>) k.getValue();
responseSchema.entrySet().forEach(a -> {
if(a.getKey().contains("schema")) {
Map<String,Object> reSchema = (HashMap<String,Object>)a.getValue();
reSchema.entrySet().forEach(c -> {
if(c.getKey().contains("items")) {
Map<String,Object> innerSchema = (HashMap<String,Object>) c.getValue();
innerSchema.remove("originalRef");
}
});
}
});
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.