-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚚 [#4980] Move implementation of JSON schema generation
openforms.forms.utils is import/export stuff, so created a dedicated module json_schema The idea is to fully define the schema generation in terms of `FormVariable`, as we have three possible options: * Formio component -> `FormVariable` with source set to component (we can get more info from the component * User-defined variable -> `FormVariable` with source set to user_defined (we can't get more info at all) * Static variable -> `FormVariable` that only exists in-memory and not in the DB, but the implementation details are outside of it Implementation-wise: * Formio component -> schema will be generated inside `FormVariable`. If it fails, fall back to basic schema based on data type. * User-defined variable -> use basic schema based on data type * Static variable -> generate schema separately, and assign it manually the `json_schema` property of a `FormVariable` instance
- Loading branch information
1 parent
e34554b
commit 57406e0
Showing
5 changed files
with
91 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Sequence, Iterator | ||
|
||
from openforms.typing import JSONObject | ||
from openforms.variables.service import get_static_variables | ||
|
||
from .models import Form, FormVariable | ||
|
||
|
||
def _iter_form_variables(form: Form) -> Iterator[FormVariable]: | ||
"""Iterate over static variables and all form variables. | ||
:param form: Form | ||
""" | ||
# Static variables are always available | ||
yield from get_static_variables() | ||
# Handle from variables holding dynamic data (component and user defined) | ||
yield from form.formvariable_set.all() | ||
|
||
|
||
def generate_json_schema(form: Form, limit_to_variables: Sequence[str]) -> JSONObject: | ||
"""Generate a JSON schema from a form, for the specified variables. | ||
:param form: The form to generate JSON schema for. | ||
:param limit_to_variables: Variables that will be included in the schema. | ||
:returns: A JSON schema representing the form variables. | ||
""" | ||
requested_variables_schema = { | ||
key: variable.as_json_schema() | ||
for variable in _iter_form_variables(form) | ||
if (key := variable.key) in limit_to_variables | ||
} | ||
|
||
# process this with deep objects etc., the FormioData data structure might be | ||
# useful here too | ||
|
||
# Result | ||
schema = { | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "object", | ||
"properties": requested_variables_schema, | ||
"required": limit_to_variables, | ||
"additionalProperties": False, | ||
} | ||
|
||
return schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters