Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/app/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import * as logger from "../../util/logger";
import * as schemaParser from "../parser/open-api/schema-parser";
import * as routeTypes from "../parser/open-api/route-types";
import * as paramGenerator from "./param-generator";
import * as paramTypes from "../parser/open-api/param-types";

const cj = require("circular-json");

export async function generateCode(
args: types.GenerateCodeInput,
Expand All @@ -27,10 +30,15 @@ export async function generateCode(
const routeComponentsArray: functionsTsGenerator.RouteComponents[] = [];

for (let route of apiTsCode.routes) {
// get the return type schema
const returnTypeSchema = routeTypes.getResponseSchema(route);
// check if the return type schema requires relaxed type tag
paramTypes.isRelaxedTypeTagRequiredForSchema(returnTypeSchema, parsedSchemaStore);

const routeComponents: functionsTsGenerator.RouteComponents = {
route: route,
params: routeTypes.getAllParamsRendered(route, parsedSchemaStore),
returnType: routeTypes.getResponseSchema(route),
returnType: returnTypeSchema,
};

routeComponentsArray.push(routeComponents);
Expand Down
69 changes: 69 additions & 0 deletions src/app/parser/open-api/param-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,33 @@ export function getEmptySchema(): Schema {
};
}

/**
* Process a Schema and all its children to determine if a relaxed type tag is required
* @param schema
* @param schemaStore
* @returns
*/
export function isRelaxedTypeTagRequiredForSchema(schema: Schema, schemaStore: ParsedSchemaStore): boolean {
if (schemaIsTypeRef(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForRefTypeSchema(schema, schemaStore);
} else if (schemaIsTypeObject(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForObjectTypeSchemaDeep(schema, schemaStore);
} else if (schemaIsTypeScalar(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForScalarTypeSchema(schema);
} else if (schemaIsTypeCustomType(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForCustomTypeSchema(schema, schemaStore);
} else if (schemaIsTypeArray(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForArrayTypeSchemaDeep(schema, schemaStore);
} else if (schemaIsTypeOneOf(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForOneOfTypeSchema(schema);
} else if (schemaIsTypeAnyOf(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForAnyOfTypeSchema(schema);
} else if (schemaIsTypeAllOf(schema)) {
schema._$requiresRelaxedTypeTag = isRelaxedTypeTagRequiredForAllOfTypeSchema(schema);
}
return schema._$requiresRelaxedTypeTag ?? false;
}

/**
* Function to determine if this scalar type schema requires
* relaxed type tag
Expand Down Expand Up @@ -334,6 +361,29 @@ export function isRelaxedTypeTagRequiredForObjectTypeSchemaShallow(
return false;
}

/**
* Function to determine if this object type schema requires
* relaxed type tag.
* This function also processes the tag requirements for its children
*
* More Reading: https://github.com/hasura/ndc-nodejs-lambda?tab=readme-ov-file#relaxed-types
*
* @param schema
*/
export function isRelaxedTypeTagRequiredForObjectTypeSchemaDeep(
schema: SchemaTypeObject,
schemaStore: ParsedSchemaStore,
): boolean {
const children = getSchemaTypeObjectChildrenMap(schema);
for (const child of Object.values(children)) {
if (isRelaxedTypeTagRequiredForSchema(child, schemaStore)) {
child._$requiresRelaxedTypeTag = true;
return true;
}
}
return false;
}

/**
* Function to determine if this array type schema requires
* relaxed type tag
Expand All @@ -353,6 +403,25 @@ export function isRelaxedTypeTagRequiredForArrayTypeSchemaShallow(
return child._$requiresRelaxedTypeTag ?? false;
}

/**
* Function to determine if this array type schema requires
* relaxed type tag
* This function also processes the tag requirements for its children
*
* More Reading: https://github.com/hasura/ndc-nodejs-lambda?tab=readme-ov-file#relaxed-types
*
* @param schema
*/
export function isRelaxedTypeTagRequiredForArrayTypeSchemaDeep(
schema: SchemaTypeArray,
schemaStore: ParsedSchemaStore,
): boolean {
const child = getSchemaTypeArrayChild(schema);
const required = isRelaxedTypeTagRequiredForSchema(child, schemaStore);
child._$requiresRelaxedTypeTag = required;
return child._$requiresRelaxedTypeTag;
}

/**
* Function to determine if this ref type schema requires relaxed type tag
*
Expand Down
76 changes: 59 additions & 17 deletions src/app/parser/open-api/route-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as logger from "../../../util/logger";
import * as paramGenerator from "../../generator/param-generator";
import { ParsedSchemaStore } from "./schema-parser";

const cj = require("circular-json");

enum RequestType {
Get = "get",
Post = "post",
Expand Down Expand Up @@ -51,6 +53,7 @@ export type ApiRoute = swaggerTypescriptApi.ParsedRoute & {
responseBodySchema: {
description: string | undefined;
contentKind: string | undefined;
contentTypes: string[] | undefined;
type: string;
content:
| {
Expand All @@ -59,6 +62,26 @@ export type ApiRoute = swaggerTypescriptApi.ParsedRoute & {
schema: paramTypes.Schema | undefined;
}
| undefined;
"application/xml":
| {
schema: paramTypes.Schema | undefined;
}
| undefined;
"text/plain":
| {
schema: paramTypes.Schema | undefined;
}
| undefined;
"plain/text":
| {
schema: paramTypes.Schema | undefined;
}
| undefined;
"*/*":
| {
schema: paramTypes.Schema | undefined;
}
| undefined;
}
| undefined;
};
Expand Down Expand Up @@ -219,6 +242,14 @@ export function hasResponseType(route: ApiRoute): boolean {
return false;
}

function getResponseSchemaUtil(route: ApiRoute): paramTypes.Schema | undefined {
return route.responseBodySchema?.content?.["application/json"]?.schema
?? route.responseBodySchema?.content?.["text/plain"]?.schema
?? route.responseBodySchema?.content?.["application/xml"]?.schema
?? route.responseBodySchema?.content?.["plain/text"]?.schema
?? route.responseBodySchema?.content?.["*/*"]?.schema;
}

export function getResponseSchema(route: ApiRoute): paramTypes.Schema {
let schemaType = route.responseBodySchema?.type;
if (!hasResponseType(route)) {
Expand All @@ -231,23 +262,34 @@ export function getResponseSchema(route: ApiRoute): paramTypes.Schema {
schemaType = "hasuraSdk.JSONValue";
}

/**
* Since there isn't a proper parser for return types
* we'll render whatever is given by the library
*/
const schema: paramTypes.SchemaTypeCustomType = {
paramName: undefined,
name: undefined,
required: true,
description: route.responseBodySchema?.description ?? "",
type: schemaType,
schema:
route.responseBodySchema?.content?.["application/json"]?.schema ??
paramTypes.getEmptySchema(),
_$rendered: schemaType,
_$forcedCustom: true,
_$requiresRelaxedTypeTag: false,
};
let schema = getResponseSchemaUtil(route);

if (!schema) {
logger.warn(
`No response schema type found for API Route ${getFormattedRouteName(route)}`,
);
logger.debug(
`Response content types for API Route ${getFormattedRouteName(route)}:\n${JSON.stringify(route.responseBodySchema?.contentTypes ?? "")}`,
);
/**
* Since there isn't a proper return type
* we'll switch the type to `hasuraSdk.JSONValue`
* and return a custom schema
*/
schema = {
paramName: undefined,
name: undefined,
required: true,
description: route.responseBodySchema?.description ?? "",
type: schemaType,
schema: paramTypes.getEmptySchema(),
_$rendered: schemaType,
_$forcedCustom: true,
_$requiresRelaxedTypeTag: false,
};
return schema;
}
schema._$rendered = schemaType;
return schema;
}

Expand Down
13 changes: 11 additions & 2 deletions src/app/parser/open-api/schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type SchemaTypeObject = {
example: string | undefined;
description: string | undefined;
$parsed: $ParsedSchema | undefined;
additionalProperties: boolean | undefined;
};

export type SchemaTypeScalar = {
Expand Down Expand Up @@ -371,7 +372,7 @@ export function getSchemaTypeSchemaChildren(
return [];
}

export function primitiveSchemaPropertiveHasAmbigousType(
export function primitiveSchemaPropertieHasAmbigousType(
property: SchemaTypePrimitive,
) {
if (property.content) {
Expand All @@ -381,19 +382,27 @@ export function primitiveSchemaPropertiveHasAmbigousType(
property.$parsed.content,
);
}
return false;
}

/**
* Checks whether a type should be marked as a RelaxedType
* More on RelaxedTypes: https://github.com/hasura/ndc-nodejs-lambda?tab=readme-ov-file#relaxed-types
*/
export function schemaPropertyIsRelaxedType(schemaProperty: SchemaProperty) {
// if the schemaProperty is an object and it can accept additional properties
// that means it has an indexed type `[key: string]: any`. This is a relaxed type
if (schemaPropertyIsTypeObject(schemaProperty)) {
if (schemaProperty.additionalProperties === true) {
return true;
}
}
if (schemaPropertyIsTypeScaler(schemaProperty)) {
if (schemaPropertyIsEnum(schemaProperty)) {
return true;
}
} else if (schemaPropertyIsTypePrimitive(schemaProperty)) {
return primitiveSchemaPropertiveHasAmbigousType(schemaProperty);
return primitiveSchemaPropertieHasAmbigousType(schemaProperty);
}
return false;
}
Expand Down