π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
fix: extract proper lookup field type
Pranav C8c37368bb34a
diff --git a/packages/nocodb/src/services/api-docs/swagger/getSwaggerColumnMetas.ts b/packages/nocodb/src/services/api-docs/swagger/getSwaggerColumnMetas.tsindex c6cfcf53c9..7752f4d2be 100644--- a/packages/nocodb/src/services/api-docs/swagger/getSwaggerColumnMetas.ts+++ b/packages/nocodb/src/services/api-docs/swagger/getSwaggerColumnMetas.ts@@ -2,9 +2,131 @@ 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 SwaggerTypes from '~/db/sql-mgr/code/routers/xc-ts/SwaggerTypes';import Noco from '~/Noco';+// Helper function to recursively get the field type for lookup columns+async function getLookupFieldType(+ context: NcContext,+ column: Column,+ base: Base,+ ncMeta = Noco.ncMeta,+): Promise<Partial<SwaggerColumn>> {+ const field: Partial<SwaggerColumn> = {+ type: 'object',+ virtual: true,+ };++ // Extract dbtype based on column source+ const dbType = await base.getSources().then((sources) => {+ const sourceId = column.source_id;+ return sources.find((s) => s.id === sourceId)?.type || sources[0]?.type;+ });++ switch (column.uidt) {+ case UITypes.Lookup:+ {+ // Recursive lookup+ const colOpt = await column.getColOptions<LookupColumn>(+ context,+ ncMeta,+ );+ if (colOpt) {+ const lookupCol = await colOpt.getLookupColumn(context);+ return await getLookupFieldType(context, lookupCol, base, ncMeta);+ }+ field.type = 'object';+ }+ break;+ case UITypes.LinkToAnotherRecord:+ {+ const colOpt = await column.getColOptions<LinkToAnotherRecordColumn>(+ context,+ ncMeta,+ );+ if (colOpt) {+ const relTable = await colOpt.getRelatedTable(context, ncMeta);+ if (colOpt.type === RelationTypes.BELONGS_TO) {+ field.type = undefined;+ field.$ref = `#/components/schemas/${relTable.title}Request`;+ } else {+ field.type = 'array';+ field.items = {+ $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.Rollup:+ field.type = 'number';+ break;+ case UITypes.Links:+ field.type = 'integer';+ break;+ case UITypes.Attachment:+ field.type = 'array';+ field.items = {+ $ref: `#/components/schemas/Attachment`,+ };+ break;+ case UITypes.Email:+ field.type = 'string';+ field.format = 'email';+ break;+ case UITypes.URL:+ field.type = 'string';+ field.format = 'uri';+ 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:+ SwaggerTypes.setSwaggerType(column, field, dbType);+ break;+ }++ return field;+}+export default async (context: NcContext,columns: Column[],@@ -78,7 +200,53 @@ export default async (}break;case UITypes.Lookup:- field.type = 'object';+ {+ const colOpt = await c.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,+ );++ // Get the type of the lookup column by recursively processing it+ const lookupField = await getLookupFieldType(+ context,+ lookupCol,+ base,+ ncMeta,+ );++ // 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 {+ // 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,+ };+ }+ }+ } else {+ // Fallback to object if we can't determine the type+ field.type = 'object';+ }+ }break;case UITypes.Rollup:field.type = 'number';diff --git a/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerColumnMetas.ts b/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerColumnMetas.tsindex 2802e90a8a..05dcf73faf 100644--- a/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerColumnMetas.ts+++ b/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerColumnMetas.ts@@ -1,9 +1,121 @@-import { UITypes } from 'nocodb-sdk';+import { UITypes, RelationTypes } from 'nocodb-sdk';import { FormulaDataTypes } from 'nocodb-sdk';import type { Base, Column, LinkToAnotherRecordColumn } from '~/models';import type { NcContext } from '~/interface/config';import SwaggerTypes from '~/db/sql-mgr/code/routers/xc-ts/SwaggerTypes';import Noco from '~/Noco';+import LookupColumn from '~/models/LookupColumn';++// Helper function to recursively get the field type for lookup columns+async function getLookupFieldType(+ context: NcContext,+ column: Column,+ base: Base,+ ncMeta = Noco.ncMeta,+): Promise<Partial<SwaggerColumn>> {+ const field: Partial<SwaggerColumn> = {+ type: 'object',+ virtual: true,+ };++ // Extract dbtype based on column source+ const dbType = await base.getSources().then((sources) => {+ const sourceId = column.source_id;+ return sources.find((s) => s.id === sourceId)?.type || sources[0]?.type;+ });++ switch (column.uidt) {+ case UITypes.Lookup:+ {+ // Recursive lookup+ const colOpt = await column.getColOptions<LookupColumn>(context, ncMeta);+ if (colOpt) {+ const lookupCol = await colOpt.getLookupColumn(context);+ return await getLookupFieldType(context, lookupCol, base, ncMeta);+ }+ field.type = 'object';+ }+ break;+ 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.Rollup:+ field.type = 'number';+ break;+ case UITypes.Links:+ field.type = 'integer';+ break;+ case UITypes.Attachment:+ field.type = 'array';+ field.items = {+ $ref: `#/components/schemas/Attachment`,+ };+ break;+ case UITypes.Email:+ field.type = 'string';+ field.format = 'email';+ break;+ case UITypes.URL:+ field.type = 'string';+ field.format = 'uri';+ 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:+ SwaggerTypes.setSwaggerType(column, field, dbType);+ break;+ }++ return field;+}export default async (context: NcContext,@@ -71,7 +183,40 @@ export default async (}break;case UITypes.Lookup:- field.type = 'object';+ {+ const colOpt = await c.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);++ // Get the type of the lookup column by recursively processing it+ const lookupField = await getLookupFieldType(context, lookupCol, base, ncMeta);++ // 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 {+ // 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+ };+ }+ }+ } else {+ // Fallback to object if we can't determine the type+ field.type = 'object';+ }+ }break;case UITypes.Rollup:field.type = 'number';