Skip to content

Conversation

@jsdbroughton
Copy link
Contributor

@jsdbroughton jsdbroughton commented Aug 18, 2024

Improvements to the JSON schema generation functionality within the speckle_automate module. The key change is enhancements to the AutomateGenerateJsonSchema class and better handling of schema properties.

Detailed Changes:

AutomateGenerateJsonSchema Class:

  • The AutomateGenerateJsonSchema class inherits directly from GenerateJsonSchema as before but now includes additional logic for handling properties and definitions ($defs).
  • The constructor has been enhanced to include an optional ref_template parameter, allowing better customisation of schema references. The default value for ref_template is #/$defs/{model}.
  • The generate method has been refactored to:
    • Improve the handling of properties and $defs within the schema.
    • Automatically process and integrate $refs within the schema using the _process_property method.

Addition of _process_property Method:
This private method handles properties within the JSON schema, particularly for processing enum, default, and type attributes. It includes logic to infer the type of properties and to handle specific scenarios where properties reference other schemas via $ref.

Enhanced Type Inference:
The _infer_type static method was added to handle type inference for various field types (bool, int, float, str). This method ensures that the correct JSON types are assigned to schema properties based on the Python field types. This was already performed to a degree, but I wanted to ensure I hadn't overridden something stupid.

Removed Redundant AutomateGenerateJsonSchema Class:
The previous version of AutomateGenerateJsonSchema has been removed to eliminate redundancy, as the new implementation now handles all required functionality.

Adds additional processing the the OOTB GenerateJsonSchema for types created in pydantic that moves the requirement from users function to the SDK
@jsdbroughton
Copy link
Contributor Author

tested with a helper function

image

def test_generate_schema(path_given="schema.json"):
    input_schema = FunctionInputs

    path = Path(path_given)
    schema = json.dumps(
        input_schema.model_json_schema(
            by_alias=True, schema_generator=AutomateGenerateJsonSchema
        )
        if input_schema
        else {}
    )
    path.write_text(schema)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants