Skip to content

Commit 7f5f308

Browse files
committed
fixed parsing
1 parent 4308cab commit 7f5f308

File tree

2 files changed

+76
-27
lines changed

2 files changed

+76
-27
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestSpec.java

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class ClientYamlSuiteRestSpec {
4444
private final Set<String> globalParameters = new HashSet<>();
4545
private final Map<String, ClientYamlSuiteRestApi> restApiMap = new HashMap<>();
4646

47-
private ClientYamlSuiteRestSpec() {}
47+
ClientYamlSuiteRestSpec() {}
4848

4949
private void addApi(ClientYamlSuiteRestApi restApi) {
5050
ClientYamlSuiteRestApi previous = restApiMap.putIfAbsent(restApi.getName(), restApi);
@@ -100,32 +100,7 @@ private static void parseSpecFile(ClientYamlSuiteRestApiParser restApiParser, Pa
100100
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
101101
String filename = jsonFile.getFileName().toString();
102102
if (filename.equals("_common.json")) {
103-
String currentFieldName = null;
104-
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
105-
if (parser.currentToken() == XContentParser.Token.FIELD_NAME) {
106-
currentFieldName = parser.currentName();
107-
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
108-
if ("params".equals(currentFieldName)) {
109-
while (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
110-
String param = parser.currentName();
111-
if (restSpec.globalParameters.contains(param)) {
112-
throw new IllegalArgumentException("Found duplicate global param [" + param + "]");
113-
}
114-
restSpec.globalParameters.add(param);
115-
parser.nextToken();
116-
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
117-
throw new IllegalArgumentException("Expected params field in rest api definition to " +
118-
"contain an object");
119-
}
120-
parser.skipChildren();
121-
}
122-
} else if ("documentation".equals(currentFieldName)) {
123-
parser.skipChildren();
124-
} else {
125-
throw new ParsingException(parser.getTokenLocation(), "unsupported field [" + currentFieldName + "]");
126-
}
127-
}
128-
}
103+
parseCommonSpec(parser, restSpec);
129104
} else {
130105
ClientYamlSuiteRestApi restApi = restApiParser.parse(jsonFile.toString(), parser);
131106
String expectedApiName = filename.substring(0, filename.lastIndexOf('.'));
@@ -140,4 +115,34 @@ private static void parseSpecFile(ClientYamlSuiteRestApiParser restApiParser, Pa
140115
throw new UncheckedIOException("Can't parse rest spec file: [" + jsonFile + "]", ex);
141116
}
142117
}
118+
119+
static void parseCommonSpec(XContentParser parser, ClientYamlSuiteRestSpec restSpec) throws IOException {
120+
String currentFieldName = null;
121+
parser.nextToken();
122+
assert parser.currentToken() == XContentParser.Token.START_OBJECT;
123+
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
124+
if (parser.currentToken() == XContentParser.Token.FIELD_NAME) {
125+
currentFieldName = parser.currentName();
126+
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
127+
if ("params".equals(currentFieldName)) {
128+
while (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
129+
String param = parser.currentName();
130+
if (restSpec.globalParameters.contains(param)) {
131+
throw new IllegalArgumentException("Found duplicate global param [" + param + "]");
132+
}
133+
restSpec.globalParameters.add(param);
134+
parser.nextToken();
135+
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
136+
throw new IllegalArgumentException("Expected params field in rest api definition to " +
137+
"contain an object");
138+
}
139+
parser.skipChildren();
140+
}
141+
} else {
142+
parser.skipChildren();
143+
}
144+
}
145+
}
146+
147+
}
143148
}

test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929

3030
public class ClientYamlSuiteRestApiTests extends ESTestCase {
3131

32+
public void testParseCommonSpec() throws IOException {
33+
XContentParser parser = createParser(YamlXContent.yamlXContent, COMMON_SPEC);
34+
ClientYamlSuiteRestSpec restSpec = new ClientYamlSuiteRestSpec();
35+
ClientYamlSuiteRestSpec.parseCommonSpec(parser, restSpec);
36+
assertTrue(restSpec.isGlobalParameter("pretty"));
37+
assertTrue(restSpec.isGlobalParameter("human"));
38+
assertTrue(restSpec.isGlobalParameter("error_trace"));
39+
assertTrue(restSpec.isGlobalParameter("source"));
40+
assertTrue(restSpec.isGlobalParameter("filter_path"));
41+
assertFalse(restSpec.isGlobalParameter("unknown"));
42+
}
43+
3244
public void testPathMatching() throws IOException {
3345
XContentParser parser = createParser(YamlXContent.yamlXContent, REST_SPEC_API);
3446
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("index.json", parser);
@@ -66,6 +78,38 @@ public void testPathMatching() throws IOException {
6678
}
6779
}
6880

81+
private static final String COMMON_SPEC = "{\n"+
82+
" \"documentation\" : {\n"+
83+
" \"url\": \"Parameters that are accepted by all API endpoints.\",\n"+
84+
" \"documentation\": \"https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html\"\n"+
85+
" },\n"+
86+
" \"params\": {\n"+
87+
" \"pretty\": {\n"+
88+
" \"type\": \"boolean\",\n"+
89+
" \"description\": \"Pretty format the returned JSON response.\",\n"+
90+
" \"default\": false\n"+
91+
" },\n"+
92+
" \"human\": {\n"+
93+
" \"type\": \"boolean\",\n"+
94+
" \"description\": \"Return human readable values for statistics.\",\n"+
95+
" \"default\": true\n"+
96+
" },\n"+
97+
" \"error_trace\": {\n"+
98+
" \"type\": \"boolean\",\n"+
99+
" \"description\": \"Include the stack trace of returned errors.\",\n"+
100+
" \"default\": false\n"+
101+
" },\n"+
102+
" \"source\": {\n"+
103+
" \"type\": \"string\",\n"+
104+
" \"description\": \"The URL-encoded request definition. Useful for libraries that do not accept a request body for non-POST requests.\"\n"+
105+
" },\n"+
106+
" \"filter_path\": {\n"+
107+
" \"type\": \"list\",\n"+
108+
" \"description\": \"A comma-separated list of filters used to reduce the response.\"\n"+
109+
" }\n"+
110+
" }\n"+
111+
"}\n";
112+
69113
private static final String REST_SPEC_API = "{\n" +
70114
" \"index\":{\n" +
71115
" \"documentation\":{\n" +

0 commit comments

Comments
 (0)