From 7c33bd9c7cff6e5fe42eb1a961c46adc82be50d2 Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Thu, 4 Jun 2026 03:37:44 +0530 Subject: [PATCH 1/3] enumOptions and enum reader --- react-openapi/components/EnhancedTable.tsx | 9 +++- react-openapi/components/FilterBar.tsx | 5 ++- react-openapi/components/fields/FormField.tsx | 44 ++++++++++--------- react-openapi/types/config.ts | 15 ++++++- react-openapi/types/overrides.ts | 10 +++-- react-openapi/utils/openapi_loader.ts | 15 +++++++ react-openapi/utils/options.ts | 28 ++++++++++++ src/openapi-config.ts | 12 +++++ 8 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 react-openapi/utils/options.ts diff --git a/react-openapi/components/EnhancedTable.tsx b/react-openapi/components/EnhancedTable.tsx index 10dc971..22be2f0 100644 --- a/react-openapi/components/EnhancedTable.tsx +++ b/react-openapi/components/EnhancedTable.tsx @@ -31,6 +31,7 @@ import VisibilityIcon from '@mui/icons-material/Visibility'; import MoreVertIcon from '@mui/icons-material/MoreVert'; import { useNavigate } from 'react-router-dom'; import { ResourceConfig } from '../types/config'; +import { getFieldOptions, toGridValueOptions } from '../utils/options'; interface EnhancedTableProps { config: ResourceConfig; @@ -96,8 +97,7 @@ export default function EnhancedTable({ } if (muiType === 'singleSelect' && field.options) { - // @ts-ignore - col.valueOptions = field.options; + col.valueOptions = toGridValueOptions(getFieldOptions(field)); } return col; @@ -379,6 +379,11 @@ function FieldRenderer({ params, field, fieldKey, config, onNavigate, navigate, if (field.type === 'datetime' || field.type === 'date') return value ? new Date(value).toLocaleString() : ''; + if (field.type === 'enum' && field.options) { + const opt = getFieldOptions(field).find(o => o.key === value); + return opt?.value ?? value; + } + if (isPk && !isMobile) { return ( (); - if (field.options) return field.options; + if (field.type === 'enum' && field.options) { + return getFieldOptions(field).map(o => o.key); + } if (!data) return []; const pull = (item: any): string | null => { diff --git a/react-openapi/components/fields/FormField.tsx b/react-openapi/components/fields/FormField.tsx index 114c88f..a194529 100644 --- a/react-openapi/components/fields/FormField.tsx +++ b/react-openapi/components/fields/FormField.tsx @@ -12,6 +12,7 @@ import { Divider, } from '@mui/material'; import { ResourceField } from '../../types/config'; +import { getFieldOptions } from '../../utils/options'; import ImageUploadField from './ImageUploadField'; interface FormFieldProps { @@ -73,40 +74,40 @@ export default function FormField({ if (field.relation && relationDataMap[field.relation]) { const relationData = relationDataMap[field.relation].data; const isArrayRelation = field.type === 'array'; - - // Determine how to display the related item - const getOptionLabel = (option: any) => { - if (!option) return ""; - if (field.displayField && option[field.displayField]) return option[field.displayField]; - // Standard naming fields - return option.name || option.title || option.label || option.id || JSON.stringify(option); - }; + const options = getFieldOptions(field, relationData); + const keyField = field.enumOption?.key ?? 'id'; - const getOptionValue = (option: any) => { - // Return the whole object to maintain identity - return option; - }; + // Normalize value: API returns whole objects on GET, but form uses key strings + const normalizedValue = (() => { + if (isArrayRelation && Array.isArray(value)) { + return value.map((v: any) => (v != null && typeof v === 'object' ? String(v[keyField] ?? '') : String(v))); + } + if (value != null && typeof value === 'object') { + return String(value[keyField] ?? ''); + } + return value ?? (isArrayRelation ? [] : ""); + })(); return ( {label} @@ -149,6 +150,7 @@ export default function FormField({ // 5. Enum Handling if (field.type === 'enum' && field.options) { + const options = getFieldOptions(field); return ( {label} @@ -158,9 +160,9 @@ export default function FormField({ onChange={(e) => onChange(e.target.value)} disabled={disabled} > - {field.options.map((opt: string) => ( - - {opt} + {options.map((opt) => ( + + {opt.value} ))} diff --git a/react-openapi/types/config.ts b/react-openapi/types/config.ts index 43be512..d0b46b3 100644 --- a/react-openapi/types/config.ts +++ b/react-openapi/types/config.ts @@ -10,6 +10,16 @@ export type FieldType = | 'object' | 'array'; +export interface SelectOption { + key: string; + value: string; +} + +export interface EnumOption { + key: string; + value: string | string[]; +} + export interface ResourceField { type: FieldType; label: string; @@ -19,8 +29,10 @@ export interface ResourceField { schema?: Record; displayField?: string | string[]; formatter?: (value: any) => string; - relation?: string; // Name of the target resource + relation?: string; filterType?: "autocomplete" | "multiselect" | "number-range" | "date-range"; + enumOption?: EnumOption; + enumLabels?: Record; } export type ResourceMode = "server" | "client"; @@ -38,6 +50,7 @@ export interface ResourceConfig { mode?: ResourceMode; fields?: string[]; }; + enumOption?: EnumOption; } export interface AppConfig { diff --git a/react-openapi/types/overrides.ts b/react-openapi/types/overrides.ts index 6200308..e3dbfe8 100644 --- a/react-openapi/types/overrides.ts +++ b/react-openapi/types/overrides.ts @@ -1,13 +1,14 @@ -/** - * This file contains application-specific overrides and configuration - * for the generic Admin Panel. - */ +export interface EnumOption { + key: string; + value: string | string[]; +} export interface FieldOverride { displayField?: string | string[]; display?: boolean; formatter?: (value: any) => string; filterType?: "autocomplete" | "multiselect" | "number-range" | "date-range"; + enumLabels?: Record; } export interface ResourceOverride { @@ -18,4 +19,5 @@ export interface ResourceOverride { mode?: "server" | "client"; fields?: string[]; }; + enumOption?: EnumOption; } diff --git a/react-openapi/utils/openapi_loader.ts b/react-openapi/utils/openapi_loader.ts index ac472f4..82ad104 100644 --- a/react-openapi/utils/openapi_loader.ts +++ b/react-openapi/utils/openapi_loader.ts @@ -80,6 +80,21 @@ function parseSchemaFields( const relation = schemaToResourceMap.get(targetSchema); if (relation) { fields[key].relation = relation; + + // Propagate enumOption from target resource config, or derive from target schema + const explicitEnumOption = configuration[relation]?.enumOption; + if (explicitEnumOption) { + fields[key].enumOption = explicitEnumOption; + } else { + const targetProps = targetSchema.properties || {}; + const valueField = Object.entries(targetProps).find( + ([name, p]: [string, any]) => name !== 'id' && p.type === 'string' + )?.[0]; + fields[key].enumOption = { + key: 'id', + value: valueField ?? 'id', + }; + } } // Recursively parse nested objects (only if not a relation) diff --git a/react-openapi/utils/options.ts b/react-openapi/utils/options.ts new file mode 100644 index 0000000..4914b3b --- /dev/null +++ b/react-openapi/utils/options.ts @@ -0,0 +1,28 @@ +import { ResourceField, SelectOption } from "../types/config"; + +export function getFieldOptions(field: ResourceField, relationData?: any[]): SelectOption[] { + if (field.type === 'enum' && field.options) { + return field.options.map(opt => ({ + key: opt, + value: field.enumLabels?.[opt] ?? opt, + })); + } + + if (field.relation) { + const data = relationData ?? []; + const enumOption = field.enumOption ?? { key: 'id', value: 'name' }; + + return data.map(item => ({ + key: String(item[enumOption.key] ?? ''), + value: Array.isArray(enumOption.value) + ? enumOption.value.map(k => item[k]).filter(v => v != null).join(' ') + : String(item[enumOption.value] ?? ''), + })); + } + + return []; +} + +export function toGridValueOptions(options: SelectOption[]): { value: string; label: string }[] { + return options.map(opt => ({ value: opt.key, label: opt.value })); +} diff --git a/src/openapi-config.ts b/src/openapi-config.ts index 638c9c4..57eb1a2 100644 --- a/src/openapi-config.ts +++ b/src/openapi-config.ts @@ -50,6 +50,18 @@ export const configuration: Record = { } }, }, + accounts: { + enumOption: { + key: 'id', + value: ['name', 'number'] + } + }, + tags: { + enumOption: { + key: 'id', + value: ['icon', 'name'] + } + }, }; export const profileConfiguration = { -- 2.49.1 From d6506e854adcadbe500a7c54b12834ec7260faec Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Thu, 4 Jun 2026 03:49:35 +0530 Subject: [PATCH 2/3] enumOptions and enum reader used everywhere --- react-openapi/components/EnhancedTable.tsx | 14 ++++++++------ react-openapi/components/FilterBar.tsx | 12 ++++++------ react-openapi/components/ResourceView.tsx | 5 +++++ react-openapi/types/config.ts | 2 +- react-openapi/types/overrides.ts | 2 +- react-openapi/utils/options.ts | 11 ++++++++--- src/openapi-config.ts | 4 ++-- 7 files changed, 31 insertions(+), 19 deletions(-) diff --git a/react-openapi/components/EnhancedTable.tsx b/react-openapi/components/EnhancedTable.tsx index 22be2f0..cad20b2 100644 --- a/react-openapi/components/EnhancedTable.tsx +++ b/react-openapi/components/EnhancedTable.tsx @@ -31,7 +31,7 @@ import VisibilityIcon from '@mui/icons-material/Visibility'; import MoreVertIcon from '@mui/icons-material/MoreVert'; import { useNavigate } from 'react-router-dom'; import { ResourceConfig } from '../types/config'; -import { getFieldOptions, toGridValueOptions } from '../utils/options'; +import { getFieldOptions, toGridValueOptions, resolveTemplate } from '../utils/options'; interface EnhancedTableProps { config: ResourceConfig; @@ -274,8 +274,9 @@ function MobileCardRow({ row, config, onDelete, onNavigate, navigate }: any) { ); } -function getFormattedDisplayValue(item: any, displayField?: string | string[]) { +function getFormattedDisplayValue(item: any, displayField?: string | string[], enumValue?: string) { if (!item) return ""; + if (enumValue) return resolveTemplate(enumValue, item); if (!displayField) return item.name || item.title || item.label || item.id || JSON.stringify(item); if (Array.isArray(displayField)) { @@ -297,7 +298,7 @@ function FieldRenderer({ params, field, fieldKey, config, onNavigate, navigate, // 1. Single Relation if (field.relation && value && !Array.isArray(value)) { const relationId = typeof value === 'object' ? (value.id || value._id || value.pk) : value; - const displayValue = getFormattedDisplayValue(value, field.displayField); + const displayValue = getFormattedDisplayValue(value, field.displayField, field.enumOption?.value); return ( getFormattedDisplayValue(item, field.displayField)).join(', '); + const enumValue = field.enumOption?.value; + const tooltipTitle = value.map((item) => getFormattedDisplayValue(item, field.displayField, enumValue)).join(', '); return ( @@ -324,7 +326,7 @@ function FieldRenderer({ params, field, fieldKey, config, onNavigate, navigate, {value.map((item, idx) => ( item[k]).filter((v) => v != null); if (parts.length > 0) return parts.join(" "); - } else { - const v = item[df]; - if (v != null) return String(v); } + const v = item[df]; + if (v != null) return String(v); - debugger; return null; }; diff --git a/react-openapi/components/ResourceView.tsx b/react-openapi/components/ResourceView.tsx index d75b439..e6ab18d 100644 --- a/react-openapi/components/ResourceView.tsx +++ b/react-openapi/components/ResourceView.tsx @@ -3,6 +3,7 @@ import { Box, Paper, CircularProgress } from '@mui/material'; import { ResourceConfig } from '../types/config'; import type { ResourceField } from '../types/config'; import { useResource } from '../hooks/useResource'; +import { resolveTemplate } from '../utils/options'; import GenericForm from './GenericForm'; import EnhancedTable from './EnhancedTable'; import FilterBar from './FilterBar'; @@ -61,6 +62,7 @@ function applyClientFilters( if (field.type === "array" && Array.isArray(itemValue)) { return itemValue.some((el: any) => { if (el != null && typeof el === "object") { + if (field.enumOption?.value) return filterValue.includes(resolveTemplate(field.enumOption.value, el)); const dispFields = getFilterDisplayFields(field); return dispFields.some((df) => filterValue.includes(String(el[df]))); } @@ -68,6 +70,7 @@ function applyClientFilters( }); } if (itemValue && typeof itemValue === "object") { + if (field.enumOption?.value) return filterValue.includes(resolveTemplate(field.enumOption.value, itemValue)); const dispFields = getFilterDisplayFields(field); const itemDisplay = dispFields.map((df) => itemValue[df]).filter((v) => v != null).join(" "); return filterValue.includes(itemDisplay); @@ -84,6 +87,7 @@ function applyClientFilters( if (field.type === "array" && Array.isArray(itemValue)) { return itemValue.some((el: any) => { if (el != null && typeof el === "object") { + if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, el) === String(filterValue); const dispFields = getFilterDisplayFields(field); return dispFields.some((df) => String(el[df]) === String(filterValue)); } @@ -92,6 +96,7 @@ function applyClientFilters( } if (itemValue && typeof itemValue === "object") { + if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, itemValue) === String(filterValue); const dispFields = getFilterDisplayFields(field); return dispFields.some((df) => String(itemValue[df]) === String(filterValue)); } diff --git a/react-openapi/types/config.ts b/react-openapi/types/config.ts index d0b46b3..b7be75d 100644 --- a/react-openapi/types/config.ts +++ b/react-openapi/types/config.ts @@ -17,7 +17,7 @@ export interface SelectOption { export interface EnumOption { key: string; - value: string | string[]; + value: string; } export interface ResourceField { diff --git a/react-openapi/types/overrides.ts b/react-openapi/types/overrides.ts index e3dbfe8..89267be 100644 --- a/react-openapi/types/overrides.ts +++ b/react-openapi/types/overrides.ts @@ -1,6 +1,6 @@ export interface EnumOption { key: string; - value: string | string[]; + value: string; } export interface FieldOverride { diff --git a/react-openapi/utils/options.ts b/react-openapi/utils/options.ts index 4914b3b..c8c3995 100644 --- a/react-openapi/utils/options.ts +++ b/react-openapi/utils/options.ts @@ -1,5 +1,12 @@ import { ResourceField, SelectOption } from "../types/config"; +export function resolveTemplate(template: string, item: any): string { + if (/\{(\w+)\}/.test(template)) { + return template.replace(/\{(\w+)\}/g, (_, field: string) => String(item[field] ?? '')); + } + return String(item[template] ?? ''); +} + export function getFieldOptions(field: ResourceField, relationData?: any[]): SelectOption[] { if (field.type === 'enum' && field.options) { return field.options.map(opt => ({ @@ -14,9 +21,7 @@ export function getFieldOptions(field: ResourceField, relationData?: any[]): Sel return data.map(item => ({ key: String(item[enumOption.key] ?? ''), - value: Array.isArray(enumOption.value) - ? enumOption.value.map(k => item[k]).filter(v => v != null).join(' ') - : String(item[enumOption.value] ?? ''), + value: resolveTemplate(enumOption.value, item), })); } diff --git a/src/openapi-config.ts b/src/openapi-config.ts index 57eb1a2..ef1a95f 100644 --- a/src/openapi-config.ts +++ b/src/openapi-config.ts @@ -53,13 +53,13 @@ export const configuration: Record = { accounts: { enumOption: { key: 'id', - value: ['name', 'number'] + value: '{name} - XXXX{number}' } }, tags: { enumOption: { key: 'id', - value: ['icon', 'name'] + value: '{icon} {name}' } }, }; -- 2.49.1 From 80ca1ac9a9d023a5d867f7c6b68136420b08839d Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Thu, 4 Jun 2026 16:17:03 +0530 Subject: [PATCH 3/3] enumOptions and enum reader used everywhere --- react-openapi/components/EnhancedTable.tsx | 4 +- react-openapi/components/FilterBar.tsx | 4 +- react-openapi/components/ResourceView.tsx | 46 ++++++-------- react-openapi/components/fields/FormField.tsx | 2 +- react-openapi/types/config.ts | 1 + react-openapi/utils/openapi_loader.ts | 60 +++++++++++++++---- react-openapi/utils/options.ts | 4 +- src/FetchRequests.tsx | 4 +- 8 files changed, 77 insertions(+), 48 deletions(-) diff --git a/react-openapi/components/EnhancedTable.tsx b/react-openapi/components/EnhancedTable.tsx index cad20b2..067e3a2 100644 --- a/react-openapi/components/EnhancedTable.tsx +++ b/react-openapi/components/EnhancedTable.tsx @@ -96,7 +96,7 @@ export default function EnhancedTable({ }; } - if (muiType === 'singleSelect' && field.options) { + if (muiType === 'singleSelect') { col.valueOptions = toGridValueOptions(getFieldOptions(field)); } @@ -381,7 +381,7 @@ function FieldRenderer({ params, field, fieldKey, config, onNavigate, navigate, if (field.type === 'datetime' || field.type === 'date') return value ? new Date(value).toLocaleString() : ''; - if (field.type === 'enum' && field.options) { + if (field.type === 'enum') { const opt = getFieldOptions(field).find(o => o.key === value); return opt?.value ?? value; } diff --git a/react-openapi/components/FilterBar.tsx b/react-openapi/components/FilterBar.tsx index 5893198..e2b8a3e 100644 --- a/react-openapi/components/FilterBar.tsx +++ b/react-openapi/components/FilterBar.tsx @@ -111,8 +111,8 @@ function extractOptions( ): string[] { const values = new Set(); - if (field.type === 'enum' && field.options) { - return getFieldOptions(field).map(o => o.key); + if (field.type === 'enum') { + return getFieldOptions(field).map(o => o.value); } if (!data) return []; diff --git a/react-openapi/components/ResourceView.tsx b/react-openapi/components/ResourceView.tsx index e6ab18d..f03885b 100644 --- a/react-openapi/components/ResourceView.tsx +++ b/react-openapi/components/ResourceView.tsx @@ -16,11 +16,16 @@ interface ResourceViewProps { import { GridPaginationModel } from '@mui/x-data-grid'; -function getFilterDisplayFields(field: ResourceField): string[] { - if (!field.displayField) return []; - return (Array.isArray(field.displayField) ? field.displayField : [field.displayField]).filter( - (df): df is string => !!df - ); +function getDisplayString(item: any, field: ResourceField): string { + if (item == null || typeof item !== 'object') return String(item ?? ''); + if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, item); + const df = field.displayField; + if (!df) return item.name ?? item.title ?? item.label ?? item.id ?? JSON.stringify(item); + if (Array.isArray(df)) { + const parts = df.map((k: string) => item[k]).filter((v: any) => v != null); + return parts.length > 0 ? parts.join(' ') : ''; + } + return String(item[df] ?? ''); } function applyClientFilters( @@ -60,20 +65,12 @@ function applyClientFilters( if (Array.isArray(filterValue)) { if (field.type === "array" && Array.isArray(itemValue)) { - return itemValue.some((el: any) => { - if (el != null && typeof el === "object") { - if (field.enumOption?.value) return filterValue.includes(resolveTemplate(field.enumOption.value, el)); - const dispFields = getFilterDisplayFields(field); - return dispFields.some((df) => filterValue.includes(String(el[df]))); - } - return filterValue.includes(String(el)); - }); + return itemValue.some((el: any) => + filterValue.includes(getDisplayString(el, field)) + ); } if (itemValue && typeof itemValue === "object") { - if (field.enumOption?.value) return filterValue.includes(resolveTemplate(field.enumOption.value, itemValue)); - const dispFields = getFilterDisplayFields(field); - const itemDisplay = dispFields.map((df) => itemValue[df]).filter((v) => v != null).join(" "); - return filterValue.includes(itemDisplay); + return filterValue.includes(getDisplayString(itemValue, field)); } return filterValue.includes(String(itemValue)); } @@ -85,20 +82,13 @@ function applyClientFilters( } if (field.type === "array" && Array.isArray(itemValue)) { - return itemValue.some((el: any) => { - if (el != null && typeof el === "object") { - if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, el) === String(filterValue); - const dispFields = getFilterDisplayFields(field); - return dispFields.some((df) => String(el[df]) === String(filterValue)); - } - return String(el) === String(filterValue); - }); + return itemValue.some((el: any) => + getDisplayString(el, field) === String(filterValue) + ); } if (itemValue && typeof itemValue === "object") { - if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, itemValue) === String(filterValue); - const dispFields = getFilterDisplayFields(field); - return dispFields.some((df) => String(itemValue[df]) === String(filterValue)); + return getDisplayString(itemValue, field) === String(filterValue); } return String(itemValue) === String(filterValue); diff --git a/react-openapi/components/fields/FormField.tsx b/react-openapi/components/fields/FormField.tsx index a194529..b1915b6 100644 --- a/react-openapi/components/fields/FormField.tsx +++ b/react-openapi/components/fields/FormField.tsx @@ -149,7 +149,7 @@ export default function FormField({ } // 5. Enum Handling - if (field.type === 'enum' && field.options) { + if (field.type === 'enum') { const options = getFieldOptions(field); return ( diff --git a/react-openapi/types/config.ts b/react-openapi/types/config.ts index b7be75d..347ab7e 100644 --- a/react-openapi/types/config.ts +++ b/react-openapi/types/config.ts @@ -57,6 +57,7 @@ export interface AppConfig { baseUrl: string; authBaseUrl: string; resources: ResourceConfig[]; + enums: Record; profile?: { resource: string; extraFields?: Record; diff --git a/react-openapi/utils/openapi_loader.ts b/react-openapi/utils/openapi_loader.ts index 82ad104..355c5f6 100644 --- a/react-openapi/utils/openapi_loader.ts +++ b/react-openapi/utils/openapi_loader.ts @@ -36,6 +36,26 @@ function mapOpenApiType(prop: any): FieldType { /** * Recursively converts OpenAPI schemas to ResourceField map */ +function mergeProperties(schema: any): { properties: Record; required: string[] } { + let properties: Record = {}; + let required: string[] = []; + + if (schema.allOf) { + for (const sub of schema.allOf) { + const merged = mergeProperties(sub); + properties = { ...properties, ...merged.properties }; + required = [...required, ...merged.required]; + } + } + if (schema.properties) { + properties = { ...properties, ...schema.properties }; + } + if (schema.required) { + required = [...required, ...schema.required]; + } + return { properties, required }; +} + function parseSchemaFields( schema: any, resourceName: string, @@ -43,12 +63,19 @@ function parseSchemaFields( configuration: Record = {} ): Record { const fields: Record = {}; - const properties = schema.properties || {}; - const required = schema.required || []; + const { properties, required } = mergeProperties(schema); const overrides = configuration[resourceName]?.fields || {}; for (const [key, prop] of Object.entries(properties) as [string, any]) { - const type = mapOpenApiType(prop); + // Resolve oneOf/anyOf by merging all branch properties + let resolvedProp = prop; + if (prop.oneOf || prop.anyOf) { + const branches = prop.oneOf || prop.anyOf; + const merged = mergeProperties({ allOf: branches }); + resolvedProp = { ...prop, type: 'object', properties: merged.properties, required: merged.required }; + } + + const type = mapOpenApiType(resolvedProp); const override = overrides[key]; // Explicitly skip 'id' as it's the primary key and handled elsewhere @@ -57,12 +84,12 @@ function parseSchemaFields( fields[key] = { type, label: - prop.title || + resolvedProp.title || key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, " "), required: required.includes(key), - options: prop.enum, + options: resolvedProp.enum, readOnly: - prop.readOnly || + resolvedProp.readOnly || key === "created_at" || key === "updated_at", ...override, @@ -71,9 +98,9 @@ function parseSchemaFields( // 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; + let targetSchema = resolvedProp; + if (type === "array" && resolvedProp.items) { + targetSchema = resolvedProp.items; } // Check if this schema object is registered as a resource @@ -98,8 +125,8 @@ function parseSchemaFields( } // Recursively parse nested objects (only if not a relation) - if (fields[key].type === "object" && prop.properties && !relation) { - fields[key].schema = parseSchemaFields(prop, resourceName, schemaToResourceMap, configuration); + if (fields[key].type === "object" && resolvedProp.properties && !relation) { + fields[key].schema = parseSchemaFields(resolvedProp, resourceName, schemaToResourceMap, configuration); } } @@ -187,6 +214,16 @@ export async function loadConfigFromOpenApi(baseUrl: string, configuration: Reco }); } + // Collect standalone enum schemas (e.g. FetchRequestStatus, AccountType, etc.) + const enums: Record = {}; + if (api.components?.schemas) { + for (const [name, schema] of Object.entries(api.components.schemas) as [string, any]) { + if (schema.enum) { + enums[name] = schema.enum; + } + } + } + // @ts-ignore const serverBaseUrl = import.meta.env.VITE_API_BASE_URL || (api.servers?.[0]?.url ?? "") // @ts-ignore @@ -195,6 +232,7 @@ export async function loadConfigFromOpenApi(baseUrl: string, configuration: Reco baseUrl: serverBaseUrl, authBaseUrl: authBaseUrl, resources, + enums, profile: profileConfiguration, }; } diff --git a/react-openapi/utils/options.ts b/react-openapi/utils/options.ts index c8c3995..13ab92a 100644 --- a/react-openapi/utils/options.ts +++ b/react-openapi/utils/options.ts @@ -8,8 +8,8 @@ export function resolveTemplate(template: string, item: any): string { } export function getFieldOptions(field: ResourceField, relationData?: any[]): SelectOption[] { - if (field.type === 'enum' && field.options) { - return field.options.map(opt => ({ + if (field.type === 'enum') { + return (field.options ?? []).map(opt => ({ key: opt, value: field.enumLabels?.[opt] ?? opt, })); diff --git a/src/FetchRequests.tsx b/src/FetchRequests.tsx index 83b492a..2abfc6f 100644 --- a/src/FetchRequests.tsx +++ b/src/FetchRequests.tsx @@ -129,7 +129,7 @@ export default function FetchRequests() { const config = useConfig(); const fetchRes = config?.resources.find((r: any) => r.name === "fetch-requests"); - const formatOptions: string[] = (fetchRes?.fields?.source?.schema?.format?.options as string[]) ?? ["axis", "icici"]; + const formatOptions: string[] = fetchRes?.fields?.source?.schema?.format?.options as string[] ?? []; const createMutation = useCreateFetchRequest(); const updateMutation = useUpdateFetchRequest(); @@ -345,7 +345,7 @@ export default function FetchRequests() { input={} renderValue={(selected) => (selected as string[]).join(", ")} > - {["pending", "processing", "paused", "raw_expenses_done", "enriched_done", "completed", "failed"].map((s) => ( + {(config?.enums?.FetchRequestStatus ?? []).map((s: string) => ( {s.replace(/_/g, " ")} ))} -- 2.49.1