-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create file + tests for parsing the type body
- Loading branch information
Showing
5 changed files
with
80 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local types = require("@server/openapi/types") | ||
|
||
type Object<T> = types.Object<T> | ||
|
||
local function parseTypeBody(object: Object<any>): Object<string | Object<any>> | ||
local body = {} | ||
|
||
if object.type then | ||
if object.type == "object" then | ||
if object.properties then | ||
for key, value in object.properties do | ||
local valueType = typeof(value) | ||
|
||
if valueType == "table" then | ||
valueType = parseTypeBody(value) | ||
end | ||
|
||
-- TODO: Handle refs | ||
|
||
if object.required then | ||
local isRequired = table.find(object.required, key) | ||
-- TODO: Check if properties in OpenAPI objects are required by | ||
-- default. Right now the assumption is that they are | ||
if not isRequired then | ||
valueType ..= "?" | ||
end | ||
end | ||
|
||
body[key] = valueType | ||
end | ||
else | ||
body = "{ [string]: any }" | ||
end | ||
else | ||
if object.type == "integer" then | ||
body = "number" | ||
else | ||
body = object.type | ||
end | ||
end | ||
end | ||
|
||
return body | ||
end | ||
|
||
return parseTypeBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
local fs = require("@lune/fs") | ||
local serde = require("@lune/serde") | ||
local openapiTypes = require("@server/openapi/openapi31") | ||
local parseTypeBody = require("./parseTypeBody") | ||
|
||
type OpenAPIObject = openapiTypes.OpenAPIObject | ||
|
||
local petstore: OpenAPIObject = serde.decode("yaml", fs.readFile("server/src/openapi/mocks/petstore.yml")) | ||
|
||
local body = parseTypeBody(petstore.components.schemas.Pet) | ||
assert(body ~= nil, `bad body (expected table, got {body})`) | ||
assert(body.id == "number", `bad body.id (expected number, got {body.id})`) | ||
assert(body.name == "string", `bad body.name (expected string, got {body.name})`) | ||
assert(body.tag == "string?", `bad body.tag (expected string?, got {body.tag})`) | ||
|
||
body = parseTypeBody(petstore.components.schemas.Pets) | ||
assert(body ~= nil, `bad body (expected table, got {body})`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters