Skip to content

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

refactor: swagger table and view schema name correction

Pranav C7beeb31fd076
diff --git a/packages/nocodb/src/services/api-docs/swagger/getPaths.ts b/packages/nocodb/src/services/api-docs/swagger/getPaths.ts
index d6d46ec697..2f26f16610 100644
--- a/packages/nocodb/src/services/api-docs/swagger/getPaths.ts
+++ b/packages/nocodb/src/services/api-docs/swagger/getPaths.ts
@@ -1,10 +1,15 @@
import { getModelPaths, getViewPaths } from './templates/paths';
-import type { Base, Model } from '~/models';
+import type { Base, Model, Source } from '~/models';
import type { SwaggerColumn } from './getSwaggerColumnMetas';
import type { SwaggerView } from './getSwaggerJSON';
import type { NcContext } from '~/interface/config';
import Noco from '~/Noco';
+// Helper function to sanitize names for use in schema names
+function sanitizeSchemaName(name: string): string {
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
+}
+
export default async function getPaths(
context: NcContext,
{
@@ -12,16 +17,20 @@ export default async function getPaths(
model,
columns,
views,
+ sourcesMap,
+ tableName,
}: {
base: Base;
model: Model;
columns: SwaggerColumn[];
views: SwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
},
_ncMeta = Noco.ncMeta,
) {
const swaggerPaths = await getModelPaths(context, {
- tableName: model.title,
+ tableName,
type: model.type,
orgs: 'v1',
columns,
@@ -35,8 +44,8 @@ export default async function getPaths(
Object.assign(
swaggerPaths,
await getViewPaths(context, {
- tableName: model.title,
- viewName: view.title,
+ tableName,
+ viewName: sanitizeSchemaName(view.title),
type: model.type,
orgs: 'v1',
columns: swaggerColumns,
diff --git a/packages/nocodb/src/services/api-docs/swagger/getSchemas.ts b/packages/nocodb/src/services/api-docs/swagger/getSchemas.ts
index b54b03fda2..1f000808b2 100644
--- a/packages/nocodb/src/services/api-docs/swagger/getSchemas.ts
+++ b/packages/nocodb/src/services/api-docs/swagger/getSchemas.ts
@@ -1,26 +1,34 @@
import { getModelSchemas, getViewSchemas } from './templates/schemas';
-import type { Base, Model } from '~/models';
+import type { Base, Model, Source } from '~/models';
import type { SwaggerColumn } from './getSwaggerColumnMetas';
import type { SwaggerView } from './getSwaggerJSON';
import Noco from '~/Noco';
+// Helper function to sanitize names for use in schema names
+function sanitizeSchemaName(name: string): string {
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
+}
+
export default async function getSchemas(
+ context,
{
base,
- model,
columns,
views,
+ tableName,
}: {
base: Base;
model: Model;
columns: SwaggerColumn[];
views: SwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
},
_ncMeta = Noco.ncMeta,
) {
const swaggerSchemas = getModelSchemas({
- tableName: model.title,
+ tableName,
orgs: 'v1',
baseName: base.title,
columns,
@@ -33,8 +41,8 @@ export default async function getSchemas(
Object.assign(
swaggerSchemas,
getViewSchemas({
- tableName: model.title,
- viewName: view.title,
+ tableName,
+ viewName: sanitizeSchemaName(view.title),
orgs: 'v1',
columns: swaggerColumns,
baseName: base.title,
diff --git a/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts b/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts
index b3afa52471..f1911e2d62 100644
--- a/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts
+++ b/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts
@@ -30,6 +30,38 @@ export default async function getSwaggerJSON(
},
};
+ // Fetch sources once for the entire base to avoid repeated queries
+ const sources = await base.getSources(false, ncMeta);
+ const sourcesMap = new Map(sources.map((source) => [source.id, source]));
+
+ // Helper function to sanitize names for use in schema names
+ function sanitizeSchemaName(name: string): string {
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
+ }
+
+ // Pre-construct table names for all models to avoid repeated construction and handle duplicates
+ const tableNamesMap = new Map<string, string>();
+ const usedTableNames = new Set<string>();
+
+ for (const model of models) {
+ const source = sourcesMap.get(model.source_id);
+ const sourcePrefix = source?.isMeta()
+ ? ''
+ : `${sanitizeSchemaName(source?.alias || 'Source')}_`;
+ const tableName = `${sourcePrefix}${sanitizeSchemaName(model.title)}`;
+
+ // Handle duplicate table names by adding a number suffix
+ let finalTableName = tableName;
+ let counter = 1;
+ while (usedTableNames.has(finalTableName)) {
+ finalTableName = `${tableName}_${counter}`;
+ counter++;
+ }
+
+ usedTableNames.add(finalTableName);
+ tableNamesMap.set(model.id, finalTableName);
+ }
+
// iterate and populate swagger schema and path for models and views
for (const model of models) {
let paths = {};
@@ -53,9 +85,31 @@ export default async function getSwaggerJSON(
// skip mm tables
if (!model.mm)
- paths = await getPaths(context, { base, model, columns, views }, ncMeta);
+ paths = await getPaths(
+ context,
+ {
+ base,
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
+ ncMeta,
+ );
- const schemas = await getSchemas({ base, model, columns, views }, ncMeta);
+ const schemas = await getSchemas(
+ context,
+ {
+ base,
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
+ ncMeta,
+ );
Object.assign(swaggerObj.paths, paths);
Object.assign(swaggerObj.components.schemas, schemas);
diff --git a/packages/nocodb/src/services/api-docs/swaggerV2/getPaths.ts b/packages/nocodb/src/services/api-docs/swaggerV2/getPaths.ts
index f1ae96b4b2..2b760e9696 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV2/getPaths.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV2/getPaths.ts
@@ -1,5 +1,5 @@
import { getModelPaths } from './templates/paths';
-import type { Model } from '~/models';
+import type { Model, Source } from '~/models';
import type { SwaggerColumn } from './getSwaggerColumnMetas';
import type { SwaggerView } from './getSwaggerJSONV2';
import type { NcContext } from '~/interface/config';
@@ -11,15 +11,19 @@ export default async function getPaths(
model,
columns,
views,
+ sourcesMap,
+ tableName,
}: {
model: Model;
columns: SwaggerColumn[];
views: SwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
},
_ncMeta = Noco.ncMeta,
) {
const swaggerPaths = await getModelPaths(context, {
- tableName: model.title,
+ tableName,
tableId: model.id,
views,
type: model.type,
diff --git a/packages/nocodb/src/services/api-docs/swaggerV2/getSchemas.ts b/packages/nocodb/src/services/api-docs/swaggerV2/getSchemas.ts
index dd6e553a92..a4ca14cb6d 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV2/getSchemas.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV2/getSchemas.ts
@@ -1,25 +1,33 @@
import { getModelSchemas } from './templates/schemas';
-import type { Base, Model } from '~/models';
+import type { Base, Model, Source } from '~/models';
import type { SwaggerColumn } from './getSwaggerColumnMetas';
import type { SwaggerView } from './getSwaggerJSONV2';
import Noco from '~/Noco';
+// Helper function to sanitize names for use in schema names
+function sanitizeSchemaName(name: string): string {
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
+}
+
export default async function getSchemas(
+ context,
{
base,
- model,
columns,
+ tableName,
}: {
base: Base;
model: Model;
columns: SwaggerColumn[];
views: SwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
},
_ncMeta = Noco.ncMeta,
) {
const swaggerSchemas = getModelSchemas({
- tableName: model.title,
+ tableName,
orgs: 'v1',
baseName: base.title,
columns,
diff --git a/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts b/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts
index b07474eecf..059cab7a7f 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts
@@ -30,6 +30,38 @@ export default async function getSwaggerJSONV2(
},
};
+ // Fetch sources once for the entire base to avoid repeated queries
+ const sources = await base.getSources(false, ncMeta);
+ const sourcesMap = new Map(sources.map((source) => [source.id, source]));
+
+ // Helper function to sanitize names for use in schema names
+ function sanitizeSchemaName(name: string): string {
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
+ }
+
+ // Pre-construct table names for all models to avoid repeated construction and handle duplicates
+ const tableNamesMap = new Map<string, string>();
+ const usedTableNames = new Set<string>();
+
+ for (const model of models) {
+ const source = sourcesMap.get(model.source_id);
+ const sourcePrefix = source?.isMeta()
+ ? ''
+ : `${sanitizeSchemaName(source?.alias || 'Source')}_`;
+ const tableName = `${sourcePrefix}${sanitizeSchemaName(model.title)}`;
+
+ // Handle duplicate table names by adding a number suffix
+ let finalTableName = tableName;
+ let counter = 1;
+ while (usedTableNames.has(finalTableName)) {
+ finalTableName = `${tableName}_${counter}`;
+ counter++;
+ }
+
+ usedTableNames.add(finalTableName);
+ tableNamesMap.set(model.id, finalTableName);
+ }
+
// iterate and populate swagger schema and path for models and views
for (const model of models) {
let paths = {};
@@ -53,9 +85,30 @@ export default async function getSwaggerJSONV2(
// skip mm tables
if (!model.mm)
- paths = await getPaths(context, { model, columns, views }, ncMeta);
+ paths = await getPaths(
+ context,
+ {
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
+ ncMeta,
+ );
- const schemas = await getSchemas({ base, model, columns, views }, ncMeta);
+ const schemas = await getSchemas(
+ context,
+ {
+ base,
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
+ ncMeta,
+ );
Object.assign(swaggerObj.paths, paths);
Object.assign(swaggerObj.components.schemas, schemas);
diff --git a/packages/nocodb/src/services/api-docs/swaggerV3/getPathsV3.ts b/packages/nocodb/src/services/api-docs/swaggerV3/getPathsV3.ts
index 99f7ed5a04..95dc375715 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV3/getPathsV3.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV3/getPathsV3.ts
@@ -1,5 +1,5 @@
import { getModelPaths } from './templates/paths';
-import type { Base, Model } from '~/models';
+import type { Base, Model, Source } from '~/models';
import type { SwaggerColumn } from './getSwaggerColumnMetasV3';
import type { SwaggerView } from './getSwaggerJSONV3';
import type { NcContext } from '~/interface/config';
@@ -12,17 +12,20 @@ export default async function getPathsV3(
model,
columns,
views,
+ tableName,
}: {
base: Base;
model: Model;
columns: SwaggerColumn[];
views: SwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
},
_ncMeta = Noco.ncMeta,
) {
const swaggerPaths = await getModelPaths(context, {
baseId: base.id,
- tableName: model.title,
+ tableName,
tableId: model.id,
views,
type: model.type,
diff --git a/packages/nocodb/src/services/api-docs/swaggerV3/getSchemasV3.ts b/packages/nocodb/src/services/api-docs/swaggerV3/getSchemasV3.ts
index 8c79675f3f..21c01583c1 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV3/getSchemasV3.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV3/getSchemasV3.ts
@@ -1,25 +1,33 @@
import { getModelSchemas } from './templates/schemas';
-import type { Base, Model } from '~/models';
+import type { Base, Model, Source } from '~/models';
import type { SwaggerColumn } from './getSwaggerColumnMetasV3';
import type { SwaggerView } from './getSwaggerJSONV3';
import Noco from '~/Noco';
+// Helper function to sanitize names for use in schema names
+function sanitizeSchemaName(name: string): string {
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
+}
+
export default async function getSchemasV3(
+ context,
{
base,
- model,
columns,
+ tableName,
}: {
base: Base;
model: Model;
columns: SwaggerColumn[];
views: SwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
},
_ncMeta = Noco.ncMeta,
) {
const swaggerSchemas = getModelSchemas({
- tableName: model.title,
+ tableName,
orgs: 'v3',
baseName: base.title,
columns,
diff --git a/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts b/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts
index 5feeadd3cd..990cb724f2 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts
@@ -30,6 +30,38 @@ export default async function getSwaggerJSONV3(
},
};
+ // Fetch sources once for the entire base to avoid repeated queries
+ const sources = await base.getSources(false, ncMeta);
+ const sourcesMap = new Map(sources.map((source) => [source.id, source]));
+
+ // Helper function to sanitize names for use in schema names
+ function sanitizeSchemaName(name: string): string {
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
+ }
+
+ // Pre-construct table names for all models to avoid repeated construction and handle duplicates
+ const tableNamesMap = new Map<string, string>();
+ const usedTableNames = new Set<string>();
+
+ for (const model of models) {
+ const source = sourcesMap.get(model.source_id);
+ const sourcePrefix = source?.isMeta()
+ ? ''
+ : `${sanitizeSchemaName(source?.alias || 'Source')}_`;
+ const tableName = `${sourcePrefix}${sanitizeSchemaName(model.title)}`;
+
+ // Handle duplicate table names by adding a number suffix
+ let finalTableName = tableName;
+ let counter = 1;
+ while (usedTableNames.has(finalTableName)) {
+ finalTableName = `${tableName}_${counter}`;
+ counter++;
+ }
+
+ usedTableNames.add(finalTableName);
+ tableNamesMap.set(model.id, finalTableName);
+ }
+
// iterate and populate swagger schema and path for models and views
for (const model of models) {
let paths = {};
@@ -55,11 +87,29 @@ export default async function getSwaggerJSONV3(
if (!model.mm)
paths = await getPathsV3(
context,
- { base, model, columns, views },
+ {
+ base,
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
ncMeta,
);
- const schemas = await getSchemasV3({ base, model, columns, views }, ncMeta);
+ const schemas = await getSchemasV3(
+ context,
+ {
+ base,
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
+ ncMeta,
+ );
Object.assign(swaggerObj.paths, paths);
Object.assign(swaggerObj.components.schemas, schemas);