Skip to content

Commit

Permalink
Merge pull request #3266 from masatake/openapi-minor-changes
Browse files Browse the repository at this point in the history
OpenAPI: dissolve #3258
  • Loading branch information
masatake committed Jan 10, 2022
2 parents 60650fa + 244a265 commit 80753d6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Units/parser-openapi.r/openapi.d/expected.tags
Original file line number Diff line number Diff line change
@@ -1,3 +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
Expand Down
2 changes: 2 additions & 0 deletions Units/parser-openapi.r/swagger.d/expected.tags
Original file line number Diff line number Diff line change
@@ -1,3 +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
Expand Down
86 changes: 73 additions & 13 deletions parsers/openapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ typedef enum {
KIND_PATH,
KIND_RESPONSE,
KIND_PARAMETER,
KIND_TITLE,
KIND_SERVER,
} openapiKind;

static kindDefinition OpenAPIKinds [] = {
{ true, 'd', "schema", "schemas" },
{ true, 'p', "path", "paths" },
{ 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
Expand All @@ -45,6 +49,11 @@ enum openapiKeys {
KEY_PARAMETERS,
KEY_RESPONSES,
KEY_DEFINITIONS,
KEY_INFO,
KEY_TITLE,
KEY_SERVERS,
KEY_URL,
KEY_HOST,
};

static const keywordTable OpenAPIKeywordTable[] = {
Expand All @@ -54,6 +63,11 @@ static const keywordTable OpenAPIKeywordTable[] = {
{ "parameters", KEY_PARAMETERS },
{ "responses", KEY_RESPONSES },
{ "definitions", KEY_DEFINITIONS },
{ "info", KEY_INFO },
{ "title", KEY_TITLE },
{ "servers", KEY_SERVERS },
{ "url", KEY_URL },
{ "host", KEY_HOST },
};

struct yamlBlockTypeStack {
Expand Down Expand Up @@ -89,8 +103,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;

Expand All @@ -102,11 +115,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,
Expand Down Expand Up @@ -197,6 +209,21 @@ static const enum openapiKeys definitions2Keys[] = {
KEY_DEFINITIONS,
};

static const enum openapiKeys title3Keys[] = {
KEY_TITLE,
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,
Expand Down Expand Up @@ -235,12 +262,30 @@ const struct tagSource tagSources[] = {
},
};

static void handleKey(struct sOpenAPISubparser *openapi,
yaml_token_t *token)
const struct tagSource tagValueSources[] = {
{
KIND_TITLE,
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,
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))
Expand All @@ -251,10 +296,23 @@ static void handleKey(struct sOpenAPISubparser *openapi,
attachYamlPosition (&tag, token, false);

makeTagEntry (&tag);
break;
}
}
}

static void handleKey(struct sOpenAPISubparser *openapi,
yaml_token_t *token)
{
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)
{
Expand All @@ -271,15 +329,17 @@ 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);
handleKey (openapi, token);
}
break;
case DSTAT_LAST_VALUE:
TRACE_PRINT(" value: %s\n", (char*)token->data.scalar.value);
TRACE_PRINT(" value: %s", (char*)token->data.scalar.value);
if (openapi->type_stack)
handleValue (openapi, token);
break;
default:
break;
Expand Down Expand Up @@ -307,9 +367,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)
Expand Down

0 comments on commit 80753d6

Please sign in to comment.