Skip to content

Commit

Permalink
airtasker#1417 date_default_schemaprop fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodin3 committed May 11, 2021
1 parent f822959 commit 0ffcb38
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,15 @@ exports[`OpenAPI 2 generator schemaprops contract with schemaprops parses correc
\\"maxLength\\": 20,
\\"pattern\\": \\"^[0-9a-z_]+$\\"
},
{
\\"name\\": \\"start-time\\",
\\"in\\": \\"header\\",
\\"description\\": \\"property-schemaprop description for date-time\\",
\\"required\\": false,
\\"type\\": \\"string\\",
\\"format\\": \\"date-time\\",
\\"default\\": \\"1990-12-31T15:59:60-08:00\\"
},
{
\\"name\\": \\"size\\",
\\"in\\": \\"header\\",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
request,
response,
String,
DateTime,
Integer,
Float,
Int64,
Expand Down Expand Up @@ -33,6 +34,11 @@ class EndpointWithSchemaPropsOnHeaders {
* "^[0-9a-z_]+$"
* */
status: String;
/** property-schemaprop description for date-time
* @default
* "1990-12-31T15:59:60-08:00"
* */
"start-time"?: DateTime;
/** property-schemaprop description for integer
* @oaSchemaProp minimum
* 1
Expand Down
9 changes: 9 additions & 0 deletions lib/src/generators/openapi2/openapi2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ describe("OpenAPI 2 generator", () => {
maxLength: 20,
pattern: "^[0-9a-z_]+$"
},
{
description: "property-schemaprop description for date-time",
name: "start-time",
in: "header",
required: false,
type: "string",
format: "date-time",
default: "1990-12-31T15:59:60-08:00"
},
{
description: "property-schemaprop description for integer",
name: "size",
Expand Down
12 changes: 12 additions & 0 deletions lib/src/generators/openapi3/__snapshots__/openapi3.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,18 @@ exports[`OpenAPI 3 generator schemaprops contract with schemaprops parses correc
\\"pattern\\": \\"^[0-9a-z_]+$\\"
}
},
{
\\"name\\": \\"start-time\\",
\\"in\\": \\"header\\",
\\"description\\": \\"property-schemaprop description for date-time\\",
\\"required\\": false,
\\"schema\\": {
\\"type\\": \\"string\\",
\\"format\\": \\"date-time\\",
\\"title\\": \\"date-time-title\\",
\\"default\\": \\"1990-12-31T15:59:60-08:00\\"
}
},
{
\\"name\\": \\"size\\",
\\"in\\": \\"header\\",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
request,
response,
String,
DateTime,
Integer,
Float,
Int64,
Expand Down Expand Up @@ -35,6 +36,13 @@ class EndpointWithSchemaPropsOnHeaders {
* "^[0-9a-z_]+$"
* */
status: String;
/** property-schemaprop description for date-time
* @oaSchemaProp title
* "date-time-title"
* @default
* "1990-12-31T15:59:60-08:00"
* */
"start-time"?: DateTime;
/** property-schemaprop description for integer
* @oaSchemaProp minimum
* 1
Expand Down
12 changes: 12 additions & 0 deletions lib/src/generators/openapi3/openapi3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ describe("OpenAPI 3 generator", () => {
pattern: "^[0-9a-z_]+$"
}
},
{
description: "property-schemaprop description for date-time",
in: "header",
name: "start-time",
required: false,
schema: {
title: "date-time-title",
type: "string",
format: "date-time",
default: "1990-12-31T15:59:60-08:00"
}
},
{
description: "property-schemaprop description for integer",
name: "size",
Expand Down
7 changes: 6 additions & 1 deletion lib/src/parsers/__spec-examples__/schemaprops.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Integer } from "../../syntax";
import { Date, Integer } from "../../syntax";

type SchemaPropTests = {
/** property-schemaprop description
Expand All @@ -17,6 +17,11 @@ type SchemaPropTests = {
* false
* */
"property-with-boolean": boolean;
/** property-schemaprop date
* @oaSchemaProp example
* "1990-12-31"
* */
"property-with-date": Date;
/**
* @oaSchemaProp example
* This_is_not_an_integer
Expand Down
18 changes: 18 additions & 0 deletions lib/src/parsers/schemaprop-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ describe("schemaprop-parser", () => {
]);
});

test("successfully parses date schemaprops on properties on type ParsedSchemaProps", () => {
const booleanSchemaPropNode = getJsDocsFromPropertySignatures(
properties,
'"property-with-date"'
);
const retrieveBooleanSchemaProp = extractJSDocSchemaProps(
booleanSchemaPropNode,
{
kind: TypeKind.DATE
}
);
expect(retrieveBooleanSchemaProp).toBeDefined();
expect(retrieveBooleanSchemaProp!.isOk).toBeTruthy();
expect(retrieveBooleanSchemaProp!.unwrapOrThrow()).toStrictEqual([
{ name: "example", value: "1990-12-31" }
]);
});

test("errors schemaprops on properties on type MismatchedSchemaPropAndIntegerType", () => {
const integerSchemaPropNode = getJsDocsFromPropertySignatures(
properties,
Expand Down
37 changes: 20 additions & 17 deletions lib/src/parsers/schemaprop-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export function extractJSDocSchemaProps(
const schemaProps: SchemaProp[] = [];
let schemaPropError;

const typeSpecified: TypeKind | "number" =
spotTypesToJSTypesMap.get(type.kind) || type.kind;

rawSchemaProps.every(schemaProp => {
const schemaPropName = schemaProp?.split("\n")[0]?.trim();
const schemaPropValue = schemaProp?.split("\n")[1]?.trim();
Expand Down Expand Up @@ -77,7 +80,7 @@ export function extractJSDocSchemaProps(

if (
(propTypeMap.get(schemaPropName)?.type === "string" ||
(type.kind === TypeKind.STRING &&
(typeSpecified === TypeKind.STRING &&
propTypeMap.get(schemaPropName)?.type === "any")) &&
(!schemaPropValue.startsWith('"') || !schemaPropValue.endsWith('"'))
) {
Expand Down Expand Up @@ -110,35 +113,22 @@ export function extractJSDocSchemaProps(
return schemaPropError;
}

const typeOf = (value: any): "string" | "number" | "boolean" => {
const typeOf = (value: any): TypeKind | "number" => {
if (/^-?\d+(\.\d+)?$/.test(value)) {
return "number";
}

if (typeof value === "boolean") {
return "boolean";
return TypeKind.BOOLEAN;
}

return "string";
return TypeKind.STRING;
};

const nameSchemaProps: string[] = schemaProps.map(ex => {
return ex.name;
});

const spotTypesToJSTypesMap = new Map();
spotTypesToJSTypesMap.set(TypeKind.INT32, "number");
spotTypesToJSTypesMap.set(TypeKind.INT64, "number");
spotTypesToJSTypesMap.set(TypeKind.INT_LITERAL, "number");
spotTypesToJSTypesMap.set(TypeKind.FLOAT, "number");
spotTypesToJSTypesMap.set(TypeKind.FLOAT_LITERAL, "number");
spotTypesToJSTypesMap.set(TypeKind.DOUBLE, "number");
spotTypesToJSTypesMap.set(TypeKind.BOOLEAN_LITERAL, "boolean");
spotTypesToJSTypesMap.set(TypeKind.STRING_LITERAL, "string");

const typeSpecified: TypeKind | "number" =
spotTypesToJSTypesMap.get(type.kind) || type.kind;

if (
schemaProps.some(
schemaProp =>
Expand Down Expand Up @@ -260,3 +250,16 @@ export const propTypeMap = new Map<
],
["uniqueItems", { type: "boolean", targetTypes: [TypeKind.ARRAY] }]
]);

const spotTypesToJSTypesMap = new Map<TypeKind, TypeKind | "number">([
[TypeKind.INT32, "number"],
[TypeKind.INT64, "number"],
[TypeKind.INT_LITERAL, "number"],
[TypeKind.FLOAT, "number"],
[TypeKind.FLOAT_LITERAL, "number"],
[TypeKind.DOUBLE, "number"],
[TypeKind.BOOLEAN_LITERAL, TypeKind.BOOLEAN],
[TypeKind.STRING_LITERAL, TypeKind.STRING],
[TypeKind.DATE, TypeKind.STRING],
[TypeKind.DATE_TIME, TypeKind.STRING]
]);

0 comments on commit 0ffcb38

Please sign in to comment.