|
| 1 | +diff --git a/src/components/molecules/BlackholeForm/molecules/FormObjectFromSwagger/FormObjectFromSwagger.tsx b/src/components/molecules/BlackholeForm/molecules/FormObjectFromSwagger/FormObjectFromSwagger.tsx |
| 2 | +index a7135d4..2fea0bb 100644 |
| 3 | +--- a/src/components/molecules/BlackholeForm/molecules/FormObjectFromSwagger/FormObjectFromSwagger.tsx |
| 4 | ++++ b/src/components/molecules/BlackholeForm/molecules/FormObjectFromSwagger/FormObjectFromSwagger.tsx |
| 5 | +@@ -68,13 +68,60 @@ export const FormObjectFromSwagger: FC<TFormObjectFromSwaggerProps> = ({ |
| 6 | + properties?: OpenAPIV2.SchemaObject['properties'] |
| 7 | + required?: string |
| 8 | + } |
| 9 | ++ |
| 10 | ++ // Check if the field name exists in additionalProperties.properties |
| 11 | ++ // If so, use the type from that property definition |
| 12 | ++ const nestedProp = addProps?.properties?.[additionalPropValue] as OpenAPIV2.SchemaObject | undefined |
| 13 | ++ let fieldType: string = addProps.type |
| 14 | ++ let fieldItems: { type: string } | undefined = addProps.items |
| 15 | ++ let fieldNestedProperties = addProps.properties || {} |
| 16 | ++ let fieldRequired: string | undefined = addProps.required |
| 17 | ++ |
| 18 | ++ if (nestedProp) { |
| 19 | ++ // Use the nested property definition if it exists |
| 20 | ++ // Handle type - it can be string or string[] in OpenAPI v2 |
| 21 | ++ if (nestedProp.type) { |
| 22 | ++ if (Array.isArray(nestedProp.type)) { |
| 23 | ++ fieldType = nestedProp.type[0] || addProps.type |
| 24 | ++ } else if (typeof nestedProp.type === 'string') { |
| 25 | ++ fieldType = nestedProp.type |
| 26 | ++ } else { |
| 27 | ++ fieldType = addProps.type |
| 28 | ++ } |
| 29 | ++ } else { |
| 30 | ++ fieldType = addProps.type |
| 31 | ++ } |
| 32 | ++ |
| 33 | ++ // Handle items - it can be ItemsObject or ReferenceObject |
| 34 | ++ if (nestedProp.items) { |
| 35 | ++ // Check if it's a valid ItemsObject with type property |
| 36 | ++ if ('type' in nestedProp.items && typeof nestedProp.items.type === 'string') { |
| 37 | ++ fieldItems = { type: nestedProp.items.type } |
| 38 | ++ } else { |
| 39 | ++ fieldItems = addProps.items |
| 40 | ++ } |
| 41 | ++ } else { |
| 42 | ++ fieldItems = addProps.items |
| 43 | ++ } |
| 44 | ++ |
| 45 | ++ fieldNestedProperties = nestedProp.properties || {} |
| 46 | ++ // Handle required field - it can be string[] in OpenAPI schema |
| 47 | ++ if (Array.isArray(nestedProp.required)) { |
| 48 | ++ fieldRequired = nestedProp.required.join(',') |
| 49 | ++ } else if (typeof nestedProp.required === 'string') { |
| 50 | ++ fieldRequired = nestedProp.required |
| 51 | ++ } else { |
| 52 | ++ fieldRequired = addProps.required |
| 53 | ++ } |
| 54 | ++ } |
| 55 | ++ |
| 56 | + inputProps?.addField({ |
| 57 | + path: Array.isArray(name) ? [...name, String(collapseTitle)] : [name, String(collapseTitle)], |
| 58 | + name: additionalPropValue, |
| 59 | +- type: addProps.type, |
| 60 | +- items: addProps.items, |
| 61 | +- nestedProperties: addProps.properties || {}, |
| 62 | +- required: addProps.required, |
| 63 | ++ type: fieldType, |
| 64 | ++ items: fieldItems, |
| 65 | ++ nestedProperties: fieldNestedProperties, |
| 66 | ++ required: fieldRequired, |
| 67 | + }) |
| 68 | + setAddditionalPropValue(undefined) |
| 69 | + } |
| 70 | +diff --git a/src/components/molecules/BlackholeForm/molecules/FormStringInput/FormStringInput.tsx b/src/components/molecules/BlackholeForm/molecules/FormStringInput/FormStringInput.tsx |
| 71 | +index 487d480..3ca46c1 100644 |
| 72 | +--- a/src/components/molecules/BlackholeForm/molecules/FormStringInput/FormStringInput.tsx |
| 73 | ++++ b/src/components/molecules/BlackholeForm/molecules/FormStringInput/FormStringInput.tsx |
| 74 | +@@ -42,7 +42,11 @@ export const FormStringInput: FC<TFormStringInputProps> = ({ |
| 75 | + const formValue = Form.useWatch(formFieldName) |
| 76 | + |
| 77 | + // Derive multiline based on current local value |
| 78 | +- const isMultiline = useMemo(() => isMultilineString(formValue), [formValue]) |
| 79 | ++ const isMultiline = useMemo(() => { |
| 80 | ++ // Normalize value for multiline check |
| 81 | ++ const value = typeof formValue === 'string' ? formValue : (formValue === null || formValue === undefined ? '' : String(formValue)) |
| 82 | ++ return isMultilineString(value) |
| 83 | ++ }, [formValue]) |
| 84 | + |
| 85 | + const title = ( |
| 86 | + <> |
| 87 | +@@ -77,6 +81,23 @@ export const FormStringInput: FC<TFormStringInputProps> = ({ |
| 88 | + rules={[{ required: forceNonRequired === false && required?.includes(getStringByName(name)) }]} |
| 89 | + validateTrigger="onBlur" |
| 90 | + hasFeedback={designNewLayout ? { icons: feedbackIcons } : true} |
| 91 | ++ normalize={(value) => { |
| 92 | ++ // Normalize value to string - prevent "[object Object]" display |
| 93 | ++ if (value === undefined || value === null) { |
| 94 | ++ return '' |
| 95 | ++ } |
| 96 | ++ if (typeof value === 'string') { |
| 97 | ++ return value |
| 98 | ++ } |
| 99 | ++ if (typeof value === 'number' || typeof value === 'boolean') { |
| 100 | ++ return String(value) |
| 101 | ++ } |
| 102 | ++ // If it's an object or array, it shouldn't be in a string field - return empty string |
| 103 | ++ if (typeof value === 'object') { |
| 104 | ++ return '' |
| 105 | ++ } |
| 106 | ++ return String(value) |
| 107 | ++ }} |
| 108 | + > |
| 109 | + <Input.TextArea |
| 110 | + placeholder={getStringByName(name)} |
| 111 | +diff --git a/src/components/molecules/BlackholeForm/organisms/BlackholeForm/helpers/casts.ts b/src/components/molecules/BlackholeForm/organisms/BlackholeForm/helpers/casts.ts |
| 112 | +index 6f9eb39..835224c 100644 |
| 113 | +--- a/src/components/molecules/BlackholeForm/organisms/BlackholeForm/helpers/casts.ts |
| 114 | ++++ b/src/components/molecules/BlackholeForm/organisms/BlackholeForm/helpers/casts.ts |
| 115 | +@@ -124,8 +124,26 @@ export const materializeAdditionalFromValues = ( |
| 116 | + * |
| 117 | + * This is used when a new field appears in the data but doesn't yet exist in the schema. |
| 118 | + */ |
| 119 | +- const makeChildFromAP = (ap: any): OpenAPIV2.SchemaObject => { |
| 120 | +- const t = ap?.type ?? 'object' |
| 121 | ++ const makeChildFromAP = (ap: any, value?: unknown): OpenAPIV2.SchemaObject => { |
| 122 | ++ // Determine type based on actual value if not explicitly defined in additionalProperties |
| 123 | ++ let t = ap?.type |
| 124 | ++ if (!t && value !== undefined && value !== null) { |
| 125 | ++ if (Array.isArray(value)) { |
| 126 | ++ t = 'array' |
| 127 | ++ } else if (typeof value === 'object') { |
| 128 | ++ t = 'object' |
| 129 | ++ } else if (typeof value === 'string') { |
| 130 | ++ t = 'string' |
| 131 | ++ } else if (typeof value === 'number') { |
| 132 | ++ t = 'number' |
| 133 | ++ } else if (typeof value === 'boolean') { |
| 134 | ++ t = 'boolean' |
| 135 | ++ } else { |
| 136 | ++ t = 'object' |
| 137 | ++ } |
| 138 | ++ } |
| 139 | ++ t = t ?? 'object' |
| 140 | ++ |
| 141 | + const child: OpenAPIV2.SchemaObject = { type: t } as any |
| 142 | + |
| 143 | + // Copy common schema details (if present) |
| 144 | +@@ -134,6 +152,20 @@ export const materializeAdditionalFromValues = ( |
| 145 | + if (ap?.required) |
| 146 | + (child as any).required = _.cloneDeep(ap.required) |
| 147 | + |
| 148 | ++ // If value is an array and items type is not defined, infer it from the first item |
| 149 | ++ if (t === 'array' && Array.isArray(value) && value.length > 0 && !ap?.items) { |
| 150 | ++ const firstItem = value[0] |
| 151 | ++ if (typeof firstItem === 'string') { |
| 152 | ++ ;(child as any).items = { type: 'string' } |
| 153 | ++ } else if (typeof firstItem === 'number') { |
| 154 | ++ ;(child as any).items = { type: 'number' } |
| 155 | ++ } else if (typeof firstItem === 'boolean') { |
| 156 | ++ ;(child as any).items = { type: 'boolean' } |
| 157 | ++ } else if (typeof firstItem === 'object') { |
| 158 | ++ ;(child as any).items = { type: 'object' } |
| 159 | ++ } |
| 160 | ++ } |
| 161 | ++ |
| 162 | + // Mark as originating from `additionalProperties` |
| 163 | + ;(child as any).isAdditionalProperties = true |
| 164 | + return child |
| 165 | +@@ -177,7 +209,16 @@ export const materializeAdditionalFromValues = ( |
| 166 | + |
| 167 | + // If the key doesn't exist in schema, create it from `additionalProperties` |
| 168 | + if (!schemaNode.properties![k]) { |
| 169 | +- schemaNode.properties![k] = makeChildFromAP(ap) |
| 170 | ++ // Check if there's a nested property definition in additionalProperties |
| 171 | ++ const nestedProp = ap?.properties?.[k] |
| 172 | ++ if (nestedProp) { |
| 173 | ++ // Use the nested property definition from additionalProperties |
| 174 | ++ schemaNode.properties![k] = _.cloneDeep(nestedProp) as any |
| 175 | ++ ;(schemaNode.properties![k] as any).isAdditionalProperties = true |
| 176 | ++ } else { |
| 177 | ++ // Create from additionalProperties with value-based type inference |
| 178 | ++ schemaNode.properties![k] = makeChildFromAP(ap, vo[k]) |
| 179 | ++ } |
| 180 | + // If it's an existing additional property, merge any nested structure |
| 181 | + } else if ((schemaNode.properties![k] as any).isAdditionalProperties && ap?.properties) { |
| 182 | + ;(schemaNode.properties![k] as any).properties ??= _.cloneDeep(ap.properties) |
| 183 | +diff --git a/src/components/molecules/BlackholeForm/organisms/BlackholeForm/utils.tsx b/src/components/molecules/BlackholeForm/organisms/BlackholeForm/utils.tsx |
| 184 | +index 2d887c7..d69d711 100644 |
| 185 | +--- a/src/components/molecules/BlackholeForm/organisms/BlackholeForm/utils.tsx |
| 186 | ++++ b/src/components/molecules/BlackholeForm/organisms/BlackholeForm/utils.tsx |
| 187 | +@@ -394,9 +394,11 @@ export const getArrayFormItemFromSwagger = ({ |
| 188 | + {(fields, { add, remove }, { errors }) => ( |
| 189 | + <> |
| 190 | + {fields.map(field => { |
| 191 | +- const fieldType = ( |
| 192 | ++ const rawFieldType = ( |
| 193 | + schema.items as (OpenAPIV2.ItemsObject & { properties?: OpenAPIV2.SchemaObject }) | undefined |
| 194 | + )?.type |
| 195 | ++ // Handle type as string or string[] (OpenAPI v2 allows both) |
| 196 | ++ const fieldType = Array.isArray(rawFieldType) ? rawFieldType[0] : rawFieldType |
| 197 | + const description = (schema.items as (OpenAPIV2.ItemsObject & { description?: string }) | undefined) |
| 198 | + ?.description |
| 199 | + const entry = schema.items as |
| 200 | +@@ -577,7 +579,29 @@ export const getArrayFormItemFromSwagger = ({ |
| 201 | + type="text" |
| 202 | + size="small" |
| 203 | + onClick={() => { |
| 204 | +- add() |
| 205 | ++ // Determine initial value based on item type |
| 206 | ++ const fieldType = ( |
| 207 | ++ schema.items as (OpenAPIV2.ItemsObject & { properties?: OpenAPIV2.SchemaObject }) | undefined |
| 208 | ++ )?.type |
| 209 | ++ |
| 210 | ++ let initialValue: unknown |
| 211 | ++ // Handle type as string or string[] (OpenAPI v2 allows both) |
| 212 | ++ const typeStr = Array.isArray(fieldType) ? fieldType[0] : fieldType |
| 213 | ++ if (typeStr === 'string') { |
| 214 | ++ initialValue = '' |
| 215 | ++ } else if (typeStr === 'number' || typeStr === 'integer') { |
| 216 | ++ initialValue = 0 |
| 217 | ++ } else if (typeStr === 'boolean') { |
| 218 | ++ initialValue = false |
| 219 | ++ } else if (typeStr === 'array') { |
| 220 | ++ initialValue = [] |
| 221 | ++ } else if (typeStr === 'object') { |
| 222 | ++ initialValue = {} |
| 223 | ++ } else { |
| 224 | ++ initialValue = '' |
| 225 | ++ } |
| 226 | ++ |
| 227 | ++ add(initialValue) |
| 228 | + }} |
| 229 | + > |
| 230 | + <PlusIcon /> |
0 commit comments