fix(ui): handle fields with single option literal

Closes #5616

Turns out the OpenAPI schema definition for a pydantic field with a `Literal` type annotation is different depending on the number of options.

When there is a single value (e.g. `Literal["foo"]`, this results in a `const` schema object. The schema parser didn't know how to handle this, and displayed a warning in the JS console.

 This situation is now handled. When a `const` schema object is encountered, we interpret that as an `EnumField` with a single option.

 I think this makes sense - if you had a truly constant value, you wouldn't make it a field, so a `const` must mean a dynamically generated enum that ended up with only a single option.
This commit is contained in:
psychedelicious 2024-02-04 17:09:01 +11:00
parent 8a61063e84
commit ce3d37e829
2 changed files with 10 additions and 0 deletions

View File

@ -373,6 +373,8 @@ const buildEnumFieldInputTemplate: FieldInputTemplateBuilder<EnumFieldInputTempl
} else {
options = firstAnyOf.enum ?? [];
}
} else if (schemaObject.const) {
options = [schemaObject.const];
} else {
options = schemaObject.enum ?? [];
}

View File

@ -55,6 +55,14 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
}
}
if (isSchemaObject(schemaObject)) {
if (schemaObject.const) {
// Fields with a single const value are defined as `Literal["value"]` in the pydantic schema - it's actually an enum
return {
name: 'EnumField',
isCollection: false,
isCollectionOrScalar: false,
};
}
if (!schemaObject.type) {
// if schemaObject has no type, then it should have one of allOf, anyOf, oneOf