Skip to content

Commit 80753d6

Browse files
authored
Merge pull request #3266 from masatake/openapi-minor-changes
OpenAPI: dissolve #3258
2 parents 60650fa + 244a265 commit 80753d6

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

Units/parser-openapi.r/openapi.d/expected.tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
test input.yaml /^ title: test$/;" t
2+
http://example.com input.yaml /^ - url: http:\/\/example.com$/;" s
13
/sample/path input.yaml /^ \/sample\/path:$/;" p
24
/sample/other/path input.yaml /^ \/sample\/other\/path:$/;" p
35
NullableField input.yaml /^ NullableField:$/;" d

Units/parser-openapi.r/swagger.d/expected.tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
test input.yaml /^ title: test$/;" t
2+
example.com input.yaml /^host: example.com$/;" s
13
/sample/path input.yaml /^ \/sample\/path:$/;" p
24
/sample/other/path input.yaml /^ \/sample\/other\/path:$/;" p
35
PolymorphicString input.yaml /^ PolymorphicString:$/;" d

parsers/openapi.c

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ typedef enum {
2828
KIND_PATH,
2929
KIND_RESPONSE,
3030
KIND_PARAMETER,
31+
KIND_TITLE,
32+
KIND_SERVER,
3133
} openapiKind;
3234

3335
static kindDefinition OpenAPIKinds [] = {
3436
{ true, 'd', "schema", "schemas" },
3537
{ true, 'p', "path", "paths" },
3638
{ true, 'R', "response", "responses" },
3739
{ true, 'P', "parameter", "parameters" },
40+
{ true, 't', "title", "titles" },
41+
{ true, 's', "server", "servers (or hosts in swagger)" },
3842
};
3943

4044
#define KEY_UNKNOWN KEYWORD_NONE
@@ -45,6 +49,11 @@ enum openapiKeys {
4549
KEY_PARAMETERS,
4650
KEY_RESPONSES,
4751
KEY_DEFINITIONS,
52+
KEY_INFO,
53+
KEY_TITLE,
54+
KEY_SERVERS,
55+
KEY_URL,
56+
KEY_HOST,
4857
};
4958

5059
static const keywordTable OpenAPIKeywordTable[] = {
@@ -54,6 +63,11 @@ static const keywordTable OpenAPIKeywordTable[] = {
5463
{ "parameters", KEY_PARAMETERS },
5564
{ "responses", KEY_RESPONSES },
5665
{ "definitions", KEY_DEFINITIONS },
66+
{ "info", KEY_INFO },
67+
{ "title", KEY_TITLE },
68+
{ "servers", KEY_SERVERS },
69+
{ "url", KEY_URL },
70+
{ "host", KEY_HOST },
5771
};
5872

5973
struct yamlBlockTypeStack {
@@ -89,8 +103,7 @@ static void pushBlockType (struct sOpenAPISubparser *openapi, yaml_token_type_t
89103
s->key = KEY_UNKNOWN;
90104
}
91105

92-
static void popBlockType (struct sOpenAPISubparser *openapi,
93-
yaml_token_t *token)
106+
static void popBlockType (struct sOpenAPISubparser *openapi)
94107
{
95108
struct yamlBlockTypeStack *s;
96109

@@ -102,11 +115,10 @@ static void popBlockType (struct sOpenAPISubparser *openapi,
102115
eFree (s);
103116
}
104117

105-
static void popAllBlockType (struct sOpenAPISubparser *openapi,
106-
yaml_token_t *token)
118+
static void popAllBlockType (struct sOpenAPISubparser *openapi)
107119
{
108120
while (openapi->type_stack)
109-
popBlockType (openapi, token);
121+
popBlockType (openapi);
110122
}
111123

112124
static bool stateStackMatch (struct yamlBlockTypeStack *stack,
@@ -197,6 +209,21 @@ static const enum openapiKeys definitions2Keys[] = {
197209
KEY_DEFINITIONS,
198210
};
199211

212+
static const enum openapiKeys title3Keys[] = {
213+
KEY_TITLE,
214+
KEY_INFO,
215+
};
216+
217+
static const enum openapiKeys server3Keys[] = {
218+
KEY_URL,
219+
KEY_UNKNOWN,
220+
KEY_SERVERS,
221+
};
222+
223+
static const enum openapiKeys host2Keys[] = {
224+
KEY_HOST,
225+
};
226+
200227
const struct tagSource tagSources[] = {
201228
{
202229
KIND_PATH,
@@ -235,12 +262,30 @@ const struct tagSource tagSources[] = {
235262
},
236263
};
237264

238-
static void handleKey(struct sOpenAPISubparser *openapi,
239-
yaml_token_t *token)
265+
const struct tagSource tagValueSources[] = {
266+
{
267+
KIND_TITLE,
268+
title3Keys,
269+
ARRAY_SIZE (title3Keys),
270+
},
271+
{
272+
KIND_SERVER,
273+
server3Keys,
274+
ARRAY_SIZE (server3Keys),
275+
},
276+
{
277+
KIND_SERVER,
278+
host2Keys,
279+
ARRAY_SIZE (host2Keys),
280+
}
281+
};
282+
283+
static void handleToken(struct sOpenAPISubparser *openapi, yaml_token_t *token,
284+
const struct tagSource *tss, size_t ts_count)
240285
{
241-
for (int i = 0; i < ARRAY_SIZE(tagSources); i++)
286+
for (int i = 0; i < ts_count; i++)
242287
{
243-
const struct tagSource* ts = &tagSources[i];
288+
const struct tagSource* ts = &tss[i];
244289

245290
if (stateStackMatch(openapi->type_stack,
246291
ts->keys, ts->countKeys))
@@ -251,10 +296,23 @@ static void handleKey(struct sOpenAPISubparser *openapi,
251296
attachYamlPosition (&tag, token, false);
252297

253298
makeTagEntry (&tag);
299+
break;
254300
}
255301
}
256302
}
257303

304+
static void handleKey(struct sOpenAPISubparser *openapi,
305+
yaml_token_t *token)
306+
{
307+
handleToken (openapi, token, tagSources, ARRAY_SIZE (tagSources));
308+
}
309+
310+
static void handleValue(struct sOpenAPISubparser *openapi,
311+
yaml_token_t *token)
312+
{
313+
handleToken (openapi, token, tagValueSources, ARRAY_SIZE (tagValueSources));
314+
}
315+
258316
static void openapiPlayStateMachine (struct sOpenAPISubparser *openapi,
259317
yaml_token_t *token)
260318
{
@@ -271,15 +329,17 @@ static void openapiPlayStateMachine (struct sOpenAPISubparser *openapi,
271329
switch (openapi->play_detection_state)
272330
{
273331
case DSTAT_LAST_KEY:
274-
TRACE_PRINT(" key: %s\n", (char*)token->data.scalar.value);
332+
TRACE_PRINT(" key: %s", (char*)token->data.scalar.value);
275333
if (openapi->type_stack)
276334
{
277335
openapi->type_stack->key = parseKey(token);
278336
handleKey (openapi, token);
279337
}
280338
break;
281339
case DSTAT_LAST_VALUE:
282-
TRACE_PRINT(" value: %s\n", (char*)token->data.scalar.value);
340+
TRACE_PRINT(" value: %s", (char*)token->data.scalar.value);
341+
if (openapi->type_stack)
342+
handleValue (openapi, token);
283343
break;
284344
default:
285345
break;
@@ -307,9 +367,9 @@ static void newTokenCallback (yamlSubparser *s, yaml_token_t *token)
307367
openapiPlayStateMachine ((struct sOpenAPISubparser *)s, token);
308368

309369
if (token->type == YAML_BLOCK_END_TOKEN)
310-
popBlockType ((struct sOpenAPISubparser *)s, token);
370+
popBlockType ((struct sOpenAPISubparser *)s);
311371
else if (token->type == YAML_STREAM_END_TOKEN)
312-
popAllBlockType ((struct sOpenAPISubparser *)s, token);
372+
popAllBlockType ((struct sOpenAPISubparser *)s);
313373
}
314374

315375
static void inputStart(subparser *s)

0 commit comments

Comments
 (0)