relation fixes
This commit is contained in:
@@ -52,7 +52,7 @@ export default function EnhancedTable({
|
||||
|
||||
// 2. Relational Link
|
||||
if (field.relation && value) {
|
||||
const relationId = typeof value === 'object' ? value.id : value;
|
||||
const relationId = typeof value === 'object' ? (value.id || value._id || value.pk) : value;
|
||||
if (relationId) {
|
||||
return (
|
||||
<Link
|
||||
@@ -60,7 +60,7 @@ export default function EnhancedTable({
|
||||
variant="body2"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onNavigateToResource?.(field.relation!, relationId);
|
||||
onNavigateToResource?.(field.relation!, String(relationId));
|
||||
}}
|
||||
>
|
||||
{relationId}
|
||||
|
||||
@@ -40,19 +40,20 @@ function mapOpenApiType(prop: any): FieldType {
|
||||
function parseSchemaFields(
|
||||
schema: any,
|
||||
resourceName: string,
|
||||
allResources: string[]
|
||||
schemaToResourceMap: Map<any, string>
|
||||
): Record<string, ResourceField> {
|
||||
const fields: Record<string, ResourceField> = {};
|
||||
const properties = schema.properties || {};
|
||||
const required = schema.required || [];
|
||||
const overrides = configuration[resourceName]?.fields || {};
|
||||
|
||||
for (const [key, prop] of Object.entries(properties) as any) {
|
||||
for (const [key, prop] of Object.entries(properties) as [string, any]) {
|
||||
const type = mapOpenApiType(prop);
|
||||
const override = overrides[key];
|
||||
|
||||
console.log("key", key, "type", type, "prop", prop, "override", override);
|
||||
if (key !== "id" && override?.display !== false) {
|
||||
// Explicitly skip 'id' as it's the primary key and handled elsewhere
|
||||
if (key === "id" || override?.display === false) continue;
|
||||
|
||||
fields[key] = {
|
||||
type,
|
||||
label:
|
||||
@@ -66,26 +67,24 @@ function parseSchemaFields(
|
||||
key === "updated_at",
|
||||
...override,
|
||||
};
|
||||
} else continue;
|
||||
|
||||
// Schema-based Relation Detection
|
||||
// If it's an object/string and matches a resource name, it might be a relation
|
||||
const potentialRelation = allResources.find(
|
||||
(res) =>
|
||||
key === res ||
|
||||
key === `${res}_id` ||
|
||||
prop.title?.toLowerCase() === res ||
|
||||
prop["x-resource"] === res
|
||||
);
|
||||
|
||||
if (potentialRelation) {
|
||||
if (type === "string" || (type === "object" && prop.properties?.id)) {
|
||||
fields[key].relation = potentialRelation;
|
||||
}
|
||||
// STRICT RELATION DETECTION
|
||||
// A field is a relation ONLY if its schema object (or items schema)
|
||||
// exactly matches a schema that is defined as a resource.
|
||||
let targetSchema = prop;
|
||||
if (type === "array" && prop.items) {
|
||||
targetSchema = prop.items;
|
||||
}
|
||||
|
||||
if (fields[key].type === "object" && prop.properties) {
|
||||
fields[key].schema = parseSchemaFields(prop, resourceName, allResources);
|
||||
// Check if this schema object is registered as a resource
|
||||
const relation = schemaToResourceMap.get(targetSchema);
|
||||
if (relation) {
|
||||
fields[key].relation = relation;
|
||||
}
|
||||
|
||||
// Recursively parse nested objects (only if not a relation)
|
||||
if (fields[key].type === "object" && prop.properties && !relation) {
|
||||
fields[key].schema = parseSchemaFields(prop, resourceName, schemaToResourceMap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +95,8 @@ function parseSchemaFields(
|
||||
* Scans paths to identify resources and their basic configuration
|
||||
*/
|
||||
export async function loadConfigFromOpenApi(baseUrl: string): Promise<AppConfig> {
|
||||
// 1. Parse and dereference the spec (handles all $ref)
|
||||
// Use SwaggerParser to dereference the spec.
|
||||
// Dereferencing preserves object identity for $ref targets.
|
||||
const api = await SwaggerParser.dereference(
|
||||
new URL("/openapi.json", baseUrl).href
|
||||
);
|
||||
@@ -104,9 +104,8 @@ export async function loadConfigFromOpenApi(baseUrl: string): Promise<AppConfig>
|
||||
const resources: ResourceConfig[] = [];
|
||||
const paths = api.paths || {};
|
||||
|
||||
// Group paths by base resource name (e.g., /expenses, /expenses/{id} -> expenses)
|
||||
// Group paths by base resource name
|
||||
const resourcePaths: Record<string, any> = {};
|
||||
|
||||
for (const path of Object.keys(paths)) {
|
||||
const base = path.split("/")[1];
|
||||
if (!base) continue;
|
||||
@@ -115,46 +114,52 @@ export async function loadConfigFromOpenApi(baseUrl: string): Promise<AppConfig>
|
||||
const methods = Object.keys(paths[path] || {});
|
||||
resourcePaths[base].methods.push(...methods);
|
||||
|
||||
// We prefer the plural GET path for schema extraction
|
||||
// Identify the list endpoint for this resource
|
||||
if (!path.includes("{") && paths[path]?.get?.responses?.["200"]) {
|
||||
resourcePaths[base].listPath = path;
|
||||
}
|
||||
}
|
||||
|
||||
const allResourceNames = Object.keys(resourcePaths);
|
||||
|
||||
// Generate ResourceConfig for each identified base path
|
||||
// 1. Identify which schema objects correspond to which resources
|
||||
const schemaToResourceMap = new Map<any, string>();
|
||||
for (const [name, info] of Object.entries(resourcePaths)) {
|
||||
const listPath = info.listPath || `/${name}`;
|
||||
const listOp = paths[listPath]?.get;
|
||||
if (!listOp) continue;
|
||||
|
||||
// Use common naming conventions or metadata from the spec
|
||||
const label = name.charAt(0).toUpperCase() + name.slice(1, -1); // naive singularization
|
||||
const pluralLabel = name.charAt(0).toUpperCase() + name.slice(1);
|
||||
|
||||
// Extract schema from the 200 response of the list endpoint
|
||||
let schema: any = null;
|
||||
const responseSchema =
|
||||
listOp.responses?.["200"]?.content?.["application/json"]?.schema;
|
||||
|
||||
const responseSchema = listOp.responses?.["200"]?.content?.["application/json"]?.schema;
|
||||
let schemaObj = responseSchema;
|
||||
if (responseSchema?.type === "array" && responseSchema.items) {
|
||||
schema = responseSchema.items;
|
||||
} else {
|
||||
schema = responseSchema;
|
||||
schemaObj = responseSchema.items;
|
||||
}
|
||||
|
||||
if (schema) {
|
||||
if (schemaObj) {
|
||||
schemaToResourceMap.set(schemaObj, name);
|
||||
resourcePaths[name].schemaObj = schemaObj;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Generate ResourceConfig for each identified resource
|
||||
for (const [name, info] of Object.entries(resourcePaths)) {
|
||||
const listPath = info.listPath || `/${name}`;
|
||||
const listOp = paths[listPath]?.get;
|
||||
if (!listOp || !info.schemaObj) continue;
|
||||
|
||||
const schema = info.schemaObj;
|
||||
const label = name.charAt(0).toUpperCase() + name.slice(1, -1);
|
||||
const pluralLabel = name.charAt(0).toUpperCase() + name.slice(1);
|
||||
|
||||
const fields = parseSchemaFields(schema, name, schemaToResourceMap);
|
||||
|
||||
resources.push({
|
||||
name,
|
||||
label: schema.title || label,
|
||||
pluralLabel: pluralLabel,
|
||||
endpoint: listPath,
|
||||
primaryKey: "id", // assume 'id' as default or look for 'required' + 'unique'
|
||||
fields: parseSchemaFields(schema, name, allResourceNames),
|
||||
primaryKey: "id", // Strict default, no heuristics
|
||||
fields,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
baseUrl:
|
||||
|
||||
Reference in New Issue
Block a user