Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix: proper lookup type extraction

Pranav Cecc7b4bb00d1
diff --git a/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerColumnMetasV3.ts b/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerColumnMetasV3.ts
index 82cec958b6..23f48eee02 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerColumnMetasV3.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerColumnMetasV3.ts
@@ -1,117 +1,209 @@
-import { UITypes } from 'nocodb-sdk';
+import { RelationTypes, UITypes } from 'nocodb-sdk';
import { FormulaDataTypes } from 'nocodb-sdk';
import type { Base, Column, LinkToAnotherRecordColumn } from '~/models';
import type { NcContext } from '~/interface/config';
+import type LookupColumn from '~/models/LookupColumn';
+import type { DriverClient } from '~/utils/nc-config';
import SwaggerTypes from '~/db/sql-mgr/code/routers/xc-ts/SwaggerTypes';
import Noco from '~/Noco';
-export default async (
+// Helper function to process a single column and return its swagger field definition
+async function processColumnToSwaggerField(
context: NcContext,
- columns: Column[],
+ column: Column,
base: Base,
ncMeta = Noco.ncMeta,
-): Promise<SwaggerColumn> => {
- // extract dbtype based on column source
- const dbType = await base.getSources().then((sources) => {
- const sourceId = columns[0]?.source_id;
- return sources.find((s) => s.id === sourceId)?.type || sources[0]?.type;
- });
- return Promise.all(
- columns.map(async (c) => {
- const field: SwaggerColumn = {
- title: c.title,
- type: 'object',
- virtual: true,
- column: c,
- };
+ isLookupHelper = false,
+ dbType: DriverClient,
+): Promise<SwaggerColumn> {
+ const field: SwaggerColumn = {
+ title: column.title,
+ type: 'object',
+ virtual: true,
+ column,
+ };
- switch (c.uidt) {
- case UITypes.LinkToAnotherRecord:
- {
- const colOpt = await c.getColOptions<LinkToAnotherRecordColumn>(
+ switch (column.uidt) {
+ case UITypes.LinkToAnotherRecord:
+ {
+ const colOpt = await column.getColOptions<LinkToAnotherRecordColumn>(
+ context,
+ ncMeta,
+ );
+ if (colOpt) {
+ const relTable = await colOpt.getRelatedTable(context, ncMeta);
+ field.type = undefined;
+ field.$ref = `#/components/schemas/${relTable.title}Request`;
+ }
+ }
+ break;
+ case UITypes.Formula:
+ // Extract type from parsed tree if available
+ if (column.colOptions?.parsed_tree?.dataType) {
+ const formulaDataType = column.colOptions.parsed_tree.dataType;
+ switch (formulaDataType) {
+ case FormulaDataTypes.NUMERIC:
+ field.type = 'number';
+ break;
+ case FormulaDataTypes.STRING:
+ field.type = 'string';
+ break;
+ case FormulaDataTypes.DATE:
+ field.type = 'string';
+ field.format = 'date-time';
+ break;
+ case FormulaDataTypes.BOOLEAN:
+ case FormulaDataTypes.LOGICAL:
+ case FormulaDataTypes.COND_EXP:
+ field.type = 'boolean';
+ break;
+ case FormulaDataTypes.NULL:
+ case FormulaDataTypes.UNKNOWN:
+ default:
+ field.type = 'string';
+ break;
+ }
+ } else {
+ // Fallback to string if no parsed tree available
+ field.type = 'string';
+ }
+ break;
+ case UITypes.Lookup:
+ if (isLookupHelper) {
+ // For recursive lookup resolution, get the underlying column type
+ const colOpt = await column.getColOptions<LookupColumn>(
+ context,
+ ncMeta,
+ );
+ if (colOpt) {
+ const lookupCol = await colOpt.getLookupColumn(context);
+ return await processColumnToSwaggerField(
+ context,
+ lookupCol,
+ base,
+ ncMeta,
+ true,
+ dbType,
+ );
+ }
+ field.type = 'object';
+ } else {
+ // For main lookup processing, determine relation type and structure
+ const colOpt = await column.getColOptions<LookupColumn>(
+ context,
+ ncMeta,
+ );
+ if (colOpt) {
+ const relationCol = await colOpt.getRelationColumn(context);
+ const lookupCol = await colOpt.getLookupColumn(context);
+ const relationColOpt =
+ await relationCol.getColOptions<LinkToAnotherRecordColumn>(
context,
ncMeta,
);
- if (colOpt) {
- const relTable = await colOpt.getRelatedTable(context, ncMeta);
- field.type = undefined;
- field.$ref = `#/components/schemas/${relTable.title}Request`;
- }
- }
- break;
- case UITypes.Formula:
- // Extract type from parsed tree if available
- if (c.colOptions?.parsed_tree?.dataType) {
- const formulaDataType = c.colOptions.parsed_tree.dataType;
- switch (formulaDataType) {
- case FormulaDataTypes.NUMERIC:
- field.type = 'number';
- break;
- case FormulaDataTypes.STRING:
- field.type = 'string';
- break;
- case FormulaDataTypes.DATE:
- field.type = 'string';
- field.format = 'date-time';
- break;
- case FormulaDataTypes.BOOLEAN:
- case FormulaDataTypes.LOGICAL:
- case FormulaDataTypes.COND_EXP:
- field.type = 'boolean';
- break;
- case FormulaDataTypes.NULL:
- case FormulaDataTypes.UNKNOWN:
- default:
- field.type = 'string';
- break;
- }
+
+ // Get the type of the lookup column by recursively processing it
+ const lookupField = await processColumnToSwaggerField(
+ context,
+ lookupCol,
+ base,
+ ncMeta,
+ true,
+ dbType,
+ );
+
+ // Determine if this is a single value or array based on relation type
+ if (
+ relationColOpt &&
+ (relationColOpt.type === RelationTypes.BELONGS_TO ||
+ relationColOpt.type === RelationTypes.ONE_TO_ONE)
+ ) {
+ // Single value lookup
+ field.type = lookupField.type;
+ field.format = lookupField.format;
+ field.$ref = lookupField.$ref;
+ field.items = lookupField.items;
} else {
- // Fallback to string if no parsed tree available
- field.type = 'string';
+ // Array lookup (HAS_MANY or MANY_TO_MANY)
+ field.type = 'array';
+ if (lookupField.$ref) {
+ field.items = { $ref: lookupField.$ref };
+ } else {
+ field.items = {
+ type: lookupField.type,
+ format: lookupField.format,
+ };
+ }
}
- break;
- case UITypes.Lookup:
- field.type = 'object';
- break;
- case UITypes.Rollup:
- field.type = 'number';
- break;
- case UITypes.Links:
- field.type = 'integer';
- break;
- case UITypes.Attachment:
- field.type = 'array';
- field.items = {
- $ref: `#/components/schemas/Attachment`,
- };
- field.virtual = false;
- break;
- case UITypes.Email:
- field.type = 'string';
- field.format = 'email';
- field.virtual = false;
- break;
- case UITypes.URL:
- field.type = 'string';
- field.format = 'uri';
- field.virtual = false;
- break;
- case UITypes.LastModifiedTime:
- case UITypes.CreatedTime:
- field.type = 'string';
- field.format = 'date-time';
- break;
- case UITypes.LastModifiedBy:
- case UITypes.CreatedBy:
+ } else {
+ // Fallback to object if we can't determine the type
field.type = 'object';
- break;
- default:
- field.virtual = false;
- SwaggerTypes.setSwaggerType(c, field, dbType);
- break;
+ }
}
+ break;
+ case UITypes.Rollup:
+ field.type = 'number';
+ break;
+ case UITypes.Links:
+ field.type = 'integer';
+ break;
+ case UITypes.Attachment:
+ field.type = 'array';
+ field.items = {
+ $ref: `#/components/schemas/Attachment`,
+ };
+ field.virtual = false;
+ break;
+ case UITypes.Email:
+ field.type = 'string';
+ field.format = 'email';
+ field.virtual = false;
+ break;
+ case UITypes.URL:
+ field.type = 'string';
+ field.format = 'uri';
+ field.virtual = false;
+ break;
+ case UITypes.LastModifiedTime:
+ case UITypes.CreatedTime:
+ field.type = 'string';
+ field.format = 'date-time';
+ break;
+ case UITypes.LastModifiedBy:
+ case UITypes.CreatedBy:
+ field.type = 'object';
+ break;
+ default:
+ field.virtual = false;
+ SwaggerTypes.setSwaggerType(column, field, dbType);
+ break;
+ }
+
+ return field;
+}
+
+export default async (
+ context: NcContext,
+ columns: Column[],
+ base: Base,
+ ncMeta = Noco.ncMeta,
+): Promise<SwaggerColumn[]> => {
+ // Extract dbtype based on column source
+ const dbType = await base.getSources().then((sources) => {
+ const sourceId = columns[0]?.source_id;
+ return sources.find((s) => s.id === sourceId)?.type || sources[0]?.type;
+ });
- return field;
+ return Promise.all(
+ columns.map(async (c) => {
+ return await processColumnToSwaggerField(
+ context,
+ c,
+ base,
+ ncMeta,
+ false,
+ dbType,
+ );
}),
);
};