From 79ce3eff961c66ae86e28edd3c90e855f8dc7b7c Mon Sep 17 00:00:00 2001 From: Vasily Kulikov Date: Thu, 6 Jan 2022 19:06:51 +0300 Subject: [PATCH 1/6] OpenAPI: remove unused parameters This change is derrived from #3258. The original commit is so large. @masatake splited the commit smaller per-topic ones. @masatake wrote this commit log. --- parsers/openapi.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/parsers/openapi.c b/parsers/openapi.c index 690f46228b..ffadd2c7b8 100644 --- a/parsers/openapi.c +++ b/parsers/openapi.c @@ -89,8 +89,7 @@ static void pushBlockType (struct sOpenAPISubparser *openapi, yaml_token_type_t s->key = KEY_UNKNOWN; } -static void popBlockType (struct sOpenAPISubparser *openapi, - yaml_token_t *token) +static void popBlockType (struct sOpenAPISubparser *openapi) { struct yamlBlockTypeStack *s; @@ -102,11 +101,10 @@ static void popBlockType (struct sOpenAPISubparser *openapi, eFree (s); } -static void popAllBlockType (struct sOpenAPISubparser *openapi, - yaml_token_t *token) +static void popAllBlockType (struct sOpenAPISubparser *openapi) { while (openapi->type_stack) - popBlockType (openapi, token); + popBlockType (openapi); } static bool stateStackMatch (struct yamlBlockTypeStack *stack, @@ -307,9 +305,9 @@ static void newTokenCallback (yamlSubparser *s, yaml_token_t *token) openapiPlayStateMachine ((struct sOpenAPISubparser *)s, token); if (token->type == YAML_BLOCK_END_TOKEN) - popBlockType ((struct sOpenAPISubparser *)s, token); + popBlockType ((struct sOpenAPISubparser *)s); else if (token->type == YAML_STREAM_END_TOKEN) - popAllBlockType ((struct sOpenAPISubparser *)s, token); + popAllBlockType ((struct sOpenAPISubparser *)s); } static void inputStart(subparser *s) From 19c0baf069d3e967c6ecbef45f702601a39e6c59 Mon Sep 17 00:00:00 2001 From: Vasily Kulikov Date: Thu, 6 Jan 2022 19:06:51 +0300 Subject: [PATCH 2/6] OpenAPI: make only one tag entry when the state stack is matched The original code tried matching even after making a tag for a token. This change is derrived from #3258. The original commit is so large. @masatake splited the commit smaller per-topic ones. @masatake wrote this commit log. --- parsers/openapi.c | 1 + 1 file changed, 1 insertion(+) diff --git a/parsers/openapi.c b/parsers/openapi.c index ffadd2c7b8..fe5b4cd9bc 100644 --- a/parsers/openapi.c +++ b/parsers/openapi.c @@ -249,6 +249,7 @@ static void handleKey(struct sOpenAPISubparser *openapi, attachYamlPosition (&tag, token, false); makeTagEntry (&tag); + break; } } } From 230b6becccc08efae3311d56bf6a168202c812c0 Mon Sep 17 00:00:00 2001 From: Vasily Kulikov Date: Thu, 6 Jan 2022 19:06:51 +0300 Subject: [PATCH 3/6] OpenAPI,refactor: make the logic of handleKey reusable This change is derrived from #3258. The original commit is so large. @masatake splited the commit smaller per-topic ones. @masatake wrote this commit log. --- parsers/openapi.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/parsers/openapi.c b/parsers/openapi.c index fe5b4cd9bc..5bc1c012fc 100644 --- a/parsers/openapi.c +++ b/parsers/openapi.c @@ -233,12 +233,12 @@ const struct tagSource tagSources[] = { }, }; -static void handleKey(struct sOpenAPISubparser *openapi, - yaml_token_t *token) +static void handleToken(struct sOpenAPISubparser *openapi, yaml_token_t *token, + const struct tagSource *tss, size_t ts_count) { - for (int i = 0; i < ARRAY_SIZE(tagSources); i++) + for (int i = 0; i < ts_count; i++) { - const struct tagSource* ts = &tagSources[i]; + const struct tagSource* ts = &tss[i]; if (stateStackMatch(openapi->type_stack, ts->keys, ts->countKeys)) @@ -254,6 +254,12 @@ static void handleKey(struct sOpenAPISubparser *openapi, } } +static void handleKey(struct sOpenAPISubparser *openapi, + yaml_token_t *token) +{ + handleToken (openapi, token, tagSources, ARRAY_SIZE (tagSources)); +} + static void openapiPlayStateMachine (struct sOpenAPISubparser *openapi, yaml_token_t *token) { From 11f8fe97734ff285164996e4ae3e8c72a23731bb Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sun, 9 Jan 2022 11:43:33 +0900 Subject: [PATCH 4/6] OpenAPI: delete redundant newline in trace output Signed-off-by: Masatake YAMATO --- parsers/openapi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parsers/openapi.c b/parsers/openapi.c index 5bc1c012fc..0eafa0dab3 100644 --- a/parsers/openapi.c +++ b/parsers/openapi.c @@ -276,7 +276,7 @@ static void openapiPlayStateMachine (struct sOpenAPISubparser *openapi, switch (openapi->play_detection_state) { case DSTAT_LAST_KEY: - TRACE_PRINT(" key: %s\n", (char*)token->data.scalar.value); + TRACE_PRINT(" key: %s", (char*)token->data.scalar.value); if (openapi->type_stack) { openapi->type_stack->key = parseKey(token); @@ -284,7 +284,7 @@ static void openapiPlayStateMachine (struct sOpenAPISubparser *openapi, } break; case DSTAT_LAST_VALUE: - TRACE_PRINT(" value: %s\n", (char*)token->data.scalar.value); + TRACE_PRINT(" value: %s", (char*)token->data.scalar.value); break; default: break; From 7ebca9d4ac607bea554413249adfc4dc5d3d9903 Mon Sep 17 00:00:00 2001 From: Vasily Kulikov Date: Thu, 6 Jan 2022 19:06:51 +0300 Subject: [PATCH 5/6] OpenAPI: extract title This change is derrived from #3258. The original commit is so large. @masatake splited the commit smaller per-topic ones. @masatake wrote this commit log. --- .../parser-openapi.r/openapi.d/expected.tags | 1 + .../parser-openapi.r/swagger.d/expected.tags | 1 + parsers/openapi.c | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/Units/parser-openapi.r/openapi.d/expected.tags b/Units/parser-openapi.r/openapi.d/expected.tags index adc34cedd5..6be1e95ebf 100644 --- a/Units/parser-openapi.r/openapi.d/expected.tags +++ b/Units/parser-openapi.r/openapi.d/expected.tags @@ -1,3 +1,4 @@ +test input.yaml /^ title: test$/;" t /sample/path input.yaml /^ \/sample\/path:$/;" p /sample/other/path input.yaml /^ \/sample\/other\/path:$/;" p NullableField input.yaml /^ NullableField:$/;" d diff --git a/Units/parser-openapi.r/swagger.d/expected.tags b/Units/parser-openapi.r/swagger.d/expected.tags index caa3932931..78d364f45e 100644 --- a/Units/parser-openapi.r/swagger.d/expected.tags +++ b/Units/parser-openapi.r/swagger.d/expected.tags @@ -1,3 +1,4 @@ +test input.yaml /^ title: test$/;" t /sample/path input.yaml /^ \/sample\/path:$/;" p /sample/other/path input.yaml /^ \/sample\/other\/path:$/;" p PolymorphicString input.yaml /^ PolymorphicString:$/;" d diff --git a/parsers/openapi.c b/parsers/openapi.c index 0eafa0dab3..60c56ed333 100644 --- a/parsers/openapi.c +++ b/parsers/openapi.c @@ -28,6 +28,7 @@ typedef enum { KIND_PATH, KIND_RESPONSE, KIND_PARAMETER, + KIND_TITLE, } openapiKind; static kindDefinition OpenAPIKinds [] = { @@ -35,6 +36,7 @@ static kindDefinition OpenAPIKinds [] = { { true, 'p', "path", "paths" }, { true, 'R', "response", "responses" }, { true, 'P', "parameter", "parameters" }, + { true, 't', "title", "titles" }, }; #define KEY_UNKNOWN KEYWORD_NONE @@ -45,6 +47,8 @@ enum openapiKeys { KEY_PARAMETERS, KEY_RESPONSES, KEY_DEFINITIONS, + KEY_INFO, + KEY_TITLE, }; static const keywordTable OpenAPIKeywordTable[] = { @@ -54,6 +58,8 @@ static const keywordTable OpenAPIKeywordTable[] = { { "parameters", KEY_PARAMETERS }, { "responses", KEY_RESPONSES }, { "definitions", KEY_DEFINITIONS }, + { "info", KEY_INFO }, + { "title", KEY_TITLE }, }; struct yamlBlockTypeStack { @@ -195,6 +201,11 @@ static const enum openapiKeys definitions2Keys[] = { KEY_DEFINITIONS, }; +static const enum openapiKeys title3Keys[] = { + KEY_TITLE, + KEY_INFO, +}; + const struct tagSource tagSources[] = { { KIND_PATH, @@ -233,6 +244,14 @@ const struct tagSource tagSources[] = { }, }; +const struct tagSource tagValueSources[] = { + { + KIND_TITLE, + title3Keys, + ARRAY_SIZE (title3Keys), + }, +}; + static void handleToken(struct sOpenAPISubparser *openapi, yaml_token_t *token, const struct tagSource *tss, size_t ts_count) { @@ -260,6 +279,12 @@ static void handleKey(struct sOpenAPISubparser *openapi, handleToken (openapi, token, tagSources, ARRAY_SIZE (tagSources)); } +static void handleValue(struct sOpenAPISubparser *openapi, + yaml_token_t *token) +{ + handleToken (openapi, token, tagValueSources, ARRAY_SIZE (tagValueSources)); +} + static void openapiPlayStateMachine (struct sOpenAPISubparser *openapi, yaml_token_t *token) { @@ -285,6 +310,8 @@ static void openapiPlayStateMachine (struct sOpenAPISubparser *openapi, break; case DSTAT_LAST_VALUE: TRACE_PRINT(" value: %s", (char*)token->data.scalar.value); + if (openapi->type_stack) + handleValue (openapi, token); break; default: break; From 244a265124c8fab279b22bb6e3445e4249f64228 Mon Sep 17 00:00:00 2001 From: Vasily Kulikov Date: Thu, 6 Jan 2022 19:06:51 +0300 Subject: [PATCH 6/6] OpenAPI: extract host (in swagger) and servers' url This change is derrived from #3258. The original commit is so large. @masatake splited the commit smaller per-topic ones. @masatake wrote this commit log. Signed-off-by: Masatake YAMATO --- .../parser-openapi.r/openapi.d/expected.tags | 1 + .../parser-openapi.r/swagger.d/expected.tags | 1 + parsers/openapi.c | 28 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/Units/parser-openapi.r/openapi.d/expected.tags b/Units/parser-openapi.r/openapi.d/expected.tags index 6be1e95ebf..050091bde5 100644 --- a/Units/parser-openapi.r/openapi.d/expected.tags +++ b/Units/parser-openapi.r/openapi.d/expected.tags @@ -1,4 +1,5 @@ test input.yaml /^ title: test$/;" t +http://example.com input.yaml /^ - url: http:\/\/example.com$/;" s /sample/path input.yaml /^ \/sample\/path:$/;" p /sample/other/path input.yaml /^ \/sample\/other\/path:$/;" p NullableField input.yaml /^ NullableField:$/;" d diff --git a/Units/parser-openapi.r/swagger.d/expected.tags b/Units/parser-openapi.r/swagger.d/expected.tags index 78d364f45e..079dda48c8 100644 --- a/Units/parser-openapi.r/swagger.d/expected.tags +++ b/Units/parser-openapi.r/swagger.d/expected.tags @@ -1,4 +1,5 @@ test input.yaml /^ title: test$/;" t +example.com input.yaml /^host: example.com$/;" s /sample/path input.yaml /^ \/sample\/path:$/;" p /sample/other/path input.yaml /^ \/sample\/other\/path:$/;" p PolymorphicString input.yaml /^ PolymorphicString:$/;" d diff --git a/parsers/openapi.c b/parsers/openapi.c index 60c56ed333..2e3cdc4aa0 100644 --- a/parsers/openapi.c +++ b/parsers/openapi.c @@ -29,6 +29,7 @@ typedef enum { KIND_RESPONSE, KIND_PARAMETER, KIND_TITLE, + KIND_SERVER, } openapiKind; static kindDefinition OpenAPIKinds [] = { @@ -37,6 +38,7 @@ static kindDefinition OpenAPIKinds [] = { { true, 'R', "response", "responses" }, { true, 'P', "parameter", "parameters" }, { true, 't', "title", "titles" }, + { true, 's', "server", "servers (or hosts in swagger)" }, }; #define KEY_UNKNOWN KEYWORD_NONE @@ -49,6 +51,9 @@ enum openapiKeys { KEY_DEFINITIONS, KEY_INFO, KEY_TITLE, + KEY_SERVERS, + KEY_URL, + KEY_HOST, }; static const keywordTable OpenAPIKeywordTable[] = { @@ -60,6 +65,9 @@ static const keywordTable OpenAPIKeywordTable[] = { { "definitions", KEY_DEFINITIONS }, { "info", KEY_INFO }, { "title", KEY_TITLE }, + { "servers", KEY_SERVERS }, + { "url", KEY_URL }, + { "host", KEY_HOST }, }; struct yamlBlockTypeStack { @@ -206,6 +214,16 @@ static const enum openapiKeys title3Keys[] = { KEY_INFO, }; +static const enum openapiKeys server3Keys[] = { + KEY_URL, + KEY_UNKNOWN, + KEY_SERVERS, +}; + +static const enum openapiKeys host2Keys[] = { + KEY_HOST, +}; + const struct tagSource tagSources[] = { { KIND_PATH, @@ -250,6 +268,16 @@ const struct tagSource tagValueSources[] = { title3Keys, ARRAY_SIZE (title3Keys), }, + { + KIND_SERVER, + server3Keys, + ARRAY_SIZE (server3Keys), + }, + { + KIND_SERVER, + host2Keys, + ARRAY_SIZE (host2Keys), + } }; static void handleToken(struct sOpenAPISubparser *openapi, yaml_token_t *token,