From 7a665448f18bce8a6a3b6bc0fe04398e4194088e Mon Sep 17 00:00:00 2001 From: Marin Minnerly Date: Sun, 4 Feb 2024 12:53:57 -0800 Subject: [PATCH] Add tests for creating comments --- server/src/openapi/createLuauComment.luau | 23 +++++++ .../src/openapi/createLuauComment.spec.luau | 31 +++++++++ .../createLuauTypeFromOpenAPIObject.luau | 65 +++++++++---------- .../createLuauTypeFromOpenAPIObject.spec.luau | 23 +++++-- server/src/openapi/mocks/commented-schema.yml | 22 +++++++ server/src/openapi/types.luau | 12 ++++ 6 files changed, 134 insertions(+), 42 deletions(-) create mode 100644 server/src/openapi/createLuauComment.luau create mode 100644 server/src/openapi/createLuauComment.spec.luau create mode 100644 server/src/openapi/mocks/commented-schema.yml create mode 100644 server/src/openapi/types.luau diff --git a/server/src/openapi/createLuauComment.luau b/server/src/openapi/createLuauComment.luau new file mode 100644 index 0000000..75937f5 --- /dev/null +++ b/server/src/openapi/createLuauComment.luau @@ -0,0 +1,23 @@ +local types = require("@server/openapi/types") + +type Object = types.Object + +local function createLuauComment(object: Object): string? + local comment = {} + if object.summary then + table.insert(comment, `-- {object.summary}`) + end + if object.summary and object.description then + table.insert(comment, "--") + end + if object.description then + table.insert(comment, `-- {object.description}`) + end + if #comment > 0 then + return table.concat(comment, "\n") + else + return nil + end +end + +return createLuauComment diff --git a/server/src/openapi/createLuauComment.spec.luau b/server/src/openapi/createLuauComment.spec.luau new file mode 100644 index 0000000..bb2acce --- /dev/null +++ b/server/src/openapi/createLuauComment.spec.luau @@ -0,0 +1,31 @@ +local fs = require("@lune/fs") +local serde = require("@lune/serde") +local openapiTypes = require("@server/openapi/openapi31") +local createLuauComment = require("./createLuauComment") + +type OpenAPIObject = openapiTypes.OpenAPIObject + +local api: OpenAPIObject = serde.decode("yaml", fs.readFile("server/src/openapi/mocks/commented-schema.yml")) + +local comment = createLuauComment(api.components.schemas.SummaryAndDescription) +local expected = [[-- A short summary +-- +-- A long-form description of the schema]] + +assert(comment, "no comment created") +assert(comment == expected, "comment doesn't match expectation\ncomment: {comment}\nexpected: {expected}") + +comment = createLuauComment(api.components.schemas.SummaryOnly) +expected = "-- A short summary" + +assert(comment, "no comment created") +assert(comment == expected, "comment doesn't match expectation\ncomment: {comment}\nexpected: {expected}") + +comment = createLuauComment(api.components.schemas.DescriptionOnly) +expected = "-- A long-form description of the schema" + +assert(comment, "no comment created") +assert(comment == expected, "comment doesn't match expectation\ncomment: {comment}\nexpected: {expected}") + +comment = createLuauComment(api.components.schemas.Empty) +assert(comment == nil, "a comment was created when it shouldn't have been") diff --git a/server/src/openapi/createLuauTypeFromOpenAPIObject.luau b/server/src/openapi/createLuauTypeFromOpenAPIObject.luau index e2c77a3..2bb2166 100644 --- a/server/src/openapi/createLuauTypeFromOpenAPIObject.luau +++ b/server/src/openapi/createLuauTypeFromOpenAPIObject.luau @@ -1,22 +1,7 @@ -local function createTypeCommentFromObject(object: Object): string? - local comment = {} - if object.summary then - table.insert(comment, `-- {object.summary}`) - end - if object.summary and object.description then - table.insert(comment, "--") - end - if object.description then - table.insert(comment, `-- {object.description}`) - end - if #comment > 0 then - return table.concat(comment, "\n") - else - return nil - end -end +local types = require("@server/openapi/types") +local createLuauComment = require("@server/openapi/createLuauComment") -type Object = { [string]: T } +type Object = types.Object local function createTypePropertiesFromObject(object: Object): Object> local inner = {} @@ -45,39 +30,49 @@ local function createTypePropertiesFromObject(object: Object): Object, -} - -local function createLuauTypeFromOpenAPIObject(object: Object): LuauType - local comment = createTypeCommentFromObject(object) +local function createLuauTypeFromOpenAPIObject(object: Object): types.LuauType + local comment = createLuauComment(object) + print(object) if object.type == "object" then local properties = {} if object.properties then for key, value in object.properties do - properties[key] = createTypePropertiesFromObject(value) - -- inner ..= `{INDENT}{key}: {createTypePropertiesFromObject(value)}?,\n` + print(key, value) + if typeof(value) == "table" then + value = createTypePropertiesFromObject(value) + else + value = typeof(value) + end + properties[key] = value end - -- inner end local inner = table.concat(properties, "\n") - print(properties) + print("properties", properties) - local typeString = `export type {name} = \{{inner}\}` - if comment then - return `{comment}\n{typeString}` - else - return typeString - end + return properties + + -- local typeString = `export type {name} = \{{inner}\}` + -- if comment then + -- return `{comment}\n{typeString}` + -- else + -- return typeString + -- end + elseif object.type == "integer" then + return "number" else return { + comment = comment, source = typeof(object), } end + + return { + name = name, + comment = createLuauComment(object), + } end return createLuauTypeFromOpenAPIObject diff --git a/server/src/openapi/createLuauTypeFromOpenAPIObject.spec.luau b/server/src/openapi/createLuauTypeFromOpenAPIObject.spec.luau index 6c2e205..65ff1a2 100644 --- a/server/src/openapi/createLuauTypeFromOpenAPIObject.spec.luau +++ b/server/src/openapi/createLuauTypeFromOpenAPIObject.spec.luau @@ -11,13 +11,22 @@ local petstore: OpenAPIObject = serde.decode("yaml", fs.readFile("server/src/ope assert(petstore.components.schemas, "no schemas found in mock API") -local schemas = createLuauTypeFromOpenAPIObject(petstore.components.schemas) - -print("schemas", schemas) - -assert(schemas.Pet, "missing type Pet") -assert(schemas.Pets, "missing type Pets") -assert(schemas.Error, "missing type Error") +-- print("schemas", petstore.components.schemas) + +local Pet = createLuauTypeFromOpenAPIObject(petstore.components.schemas.Pet) +assert(Pet ~= nil, `missing "Pet" schema`) +assert(Pet.id == "number", `bad Pet.id (expected number, got {Pet.id})`) +assert(Pet.name == "string", `bad Pet.name (expected string, got {Pet.name})`) +assert(Pet.tag == "string?", `bad Pet.tag (expected string?, got {Pet.tag})`) + +local Pets = createLuauTypeFromOpenAPIObject(petstore.components.schemas.Pets) +assert(Pets ~= nil, `missing "Pets" schema`) +assert(Pets.id == "number", `bad Pets.id (expected number, got {Pets.id})`) +assert(Pets.name == "string", `bad Pets.name (expected string, got {Pets.name})`) +assert(Pets.tag == "string?", `bad Pets.tag (expected string?, got {Pets.tag})`) + +local Error = createLuauTypeFromOpenAPIObject(petstore.components.schemas.Error) +assert(Error, `missing "Error" schema`) -- assert(nil, "summary is not included in type comment") -- assert(nil, "description is not included in type comment") diff --git a/server/src/openapi/mocks/commented-schema.yml b/server/src/openapi/mocks/commented-schema.yml new file mode 100644 index 0000000..00d28f2 --- /dev/null +++ b/server/src/openapi/mocks/commented-schema.yml @@ -0,0 +1,22 @@ +openapi: 3.1.0 + +info: + title: Title + description: Description + version: 1.0.0 + basePath: /v1 + +components: + schemas: + SummaryAndDescription: + summary: "A short summary" + description: "A long-form description of the schema" + + SummaryOnly: + summary: "A short summary" + + DescriptionOnly: + description: "A long-form description of the schema" + + Empty: + foo: true diff --git a/server/src/openapi/types.luau b/server/src/openapi/types.luau new file mode 100644 index 0000000..b24d09b --- /dev/null +++ b/server/src/openapi/types.luau @@ -0,0 +1,12 @@ +export type Object = { [string]: T } + +export type LuauType = { + name: string, + body: Primitive | { [string]: Primitive }, + comment: string?, + unions: { string }?, + isExported: boolean?, + isRequired: boolean?, +} + +return {}