apisix-dashboard icon indicating copy to clipboard operation
apisix-dashboard copied to clipboard

GSOC 26 Proof of Concept: add SchemaForm component for JSON Schema to Form UI

Open DSingh0304 opened this issue 10 months ago • 15 comments

JSON Schema Form UI: anyOf, if/then/else, patternProperties & Form State Cleanup

Closes #3315 Closes #2986

Summary

This PR is a proof of concept for the GSoC 2026 "Dynamic Plugin Configuration UI" project. It demonstrates a production-ready, recursive form engine that handles complex JSON Schema Draft 7 constructs.

Beyond adding new keywords, this update focuses on Correctness and Data Integrity, ensuring that the form state sent to the APISIX backend is always minimal and strictly adheres to the active schema branch.

What's Changed (Recent Fixes & Polish)

1. Automatic Form State Cleanup (High Priority)

Implemented unregister() logic in OneOfFields, AnyOfFields, and IfThenElseFields.

  • The Problem: Previously, switching from a redis backend to a local backend would leave "ghost" values (like redis_host) in the final submit payload, potentially causing backend validation failures.
  • The Fix: Stale fields are now instantly removed from the form state the moment a branch is deselected. This ensures "Submit Payload Consistency" as requested by maintainers.

2. patternProperties Support (Dynamic Key-Value Maps)

Many APISIX plugins (like proxy-rewrite or response-rewrite) require dynamic HTTP header configuration.

  • Added the PatternPropertiesField component to handle schemas where keys are regex-based strings.
  • Included the serializePatternProperties utility to transform internal form state into the flat object structure required by the APISIX Admin API.

3. Smart Label Formatting

Refined the formatLabel utility to handle technical acronyms. Fields like oauth_client_id or api_key_uri now render professionally as "OAuth Client ID" and "API Key URI" instead of camel-cased defaults.

4. Clean Build (Zero Warnings)

Resolved all linting errors and warnings across the codebase.

  • Refactored SchemaForm.tsx to resolve Fast Refresh issues.
  • Fixed pre-existing Playwright E2E warnings using non-null assertions.
  • Validated with pnpm lint and --max-warnings=0.

Core Engine Features

1. anyOf conditional field groups (AnyOfFields)

Automatically detects discriminator const values and renders the active branch's fields reactively. Essential for the limit-count plugin's backend selection.

2. if / then / else conditional rendering (IfThenElseFields)

Uses AJV to evaluate nested schemas against live form data. Powers toggles like "Enable Burst Mode" in the limit-conn plugin.

3. encrypt_fields masking

Automatically transforms TextInput into masked PasswordInput for sensitive keys like jwt-auth secrets.


Tests - 50 Passing (0 Failures, 0 Warnings)

This PR includes the project's first comprehensive unit and integration test suite for SchemaForm:

  • validation.test.ts — AJAX schema resolution and logic.
  • SchemaField.test.tsx — Widget mapping and labels.
  • SchemaForm.test.tsx — Integration testing for conditional rendering and Form State Cleanup.
Test Files  3 passed (3)
Tests       50 passed (50)
Duration    ~2s

Interactive Demo

Accessible at http://localhost:5173/ui/schema_form_demo. Features a "Live Form Data" monitor that allows maintainers to verify that fields are purged from the JSON payload in real-time as branches are toggled.

Video Proof

Screen recording demonstrating the implemented features live on the development server.

https://github.com/user-attachments/assets/ad3bc396-af68-4c08-b543-e3414e675355

What the recording shows:

  • Cleanup Logic: Toggling "Enable Burst Mode" and seeing fields being added/removed from the live JSON block instantly.
  • PatternProperties: Adding and removing custom HTTP headers in the proxy-rewrite demo.
  • Green Build: Running pnpm test and pnpm lint to show a perfectly clean environment.

DSingh0304 avatar Jan 05 '26 06:01 DSingh0304

@Baoyuantop Is this approach aligned with the project's direction?

DSingh0304 avatar Jan 10 '26 04:01 DSingh0304

This may require more time to discuss.

SkyeYoung avatar Jan 12 '26 03:01 SkyeYoung

This may require more time to discuss.

Yes sir sure ...

DSingh0304 avatar Jan 12 '26 06:01 DSingh0304

Hi @Baoyuantop @SkyeYoung I've submitted this PoC PR for this issue as part of my GSoC 2026 application journey.

The branch adds anyOf, if/then/else, and encrypt_fields support to the existing SchemaForm, with 43 passing tests and an interactive demo at /schema_form_demo. A developer guide is included at schema-form.md.

Happy to discuss any implementation decisions or adjust based on feedback. Thank you!

DSingh0304 avatar Mar 03 '26 15:03 DSingh0304

Hi @DSingh0304

I tested the SchemaForm demo locally from this PR branch.

Steps to reproduce:

  1. Navigate to /ui/schema_form_demo
  2. Select auth_type = oauth
  3. Fill the OAuth fields
  4. Change auth_type to api_key

Expected behavior: When switching to api_key, OAuth-related fields should be removed from the form state.

Actual behavior: The OAuth fields remain in the form data even after switching to api_key.

Example form data after switching:

{ "auth_type": "api_key", "oauth_client_id": "...", "oauth_client_secret": "..." }

It seems the previous oneOf branch values remain in the react-hook-form state.

Screenshot attached for reference. Screenshot 2026-03-05 173751 Screenshot 2026-03-05 173830 Screenshot 2026-03-05 173858 Screenshot 2026-03-05 173935 Screenshot 2026-03-05 174003 Screenshot 2026-03-05 174021

Let me know if you would like me to explore a potential fix.

suryaparua-official avatar Mar 05 '26 12:03 suryaparua-official

I also tested enabling shouldUnregister: true in useForm during local testing, and it removed the stale oneOf fields when switching variants.

It seems this behavior may be related to React Hook Form keeping unmounted fields in form state by default.

suryaparua-official avatar Mar 05 '26 12:03 suryaparua-official

Hi @suryaparua-official I have been testing the form myself and I came across these issue. I will resolve them soon, I have been busy. Thank you for pointing the issues out. They will be resolved soon.

DSingh0304 avatar Mar 05 '26 12:03 DSingh0304

Hi @DSingh0304,

I tested the SchemaForm demo and was able to reproduce the issue where switching between schema branches (oneOf / anyOf / if-then-else) retains stale field values in the form state.

I worked on a fix for this by implementing branch-aware cleanup inside SchemaForm. Instead of relying only on shouldUnregister, the solution explicitly unregisters fields that are no longer part of the active schema branch.

Solution

  • Tracks active schema branch paths
  • Automatically calls unregister on fields from previous branches
  • Works for:
    • oneOf
    • anyOf
    • if/then/else
    • dependencies

Testing

  • Verified via /schema_form_demo
  • Switching branches correctly removes stale values
  • No regression in existing behavior

I’d be happy to open a PR or contribute to the existing one if this approach aligns with the project direction. Please let me know your thoughts.

Jaswanth-arjun avatar Mar 24 '26 11:03 Jaswanth-arjun

Hi @DSingh0304,

I tested the SchemaForm demo and was able to reproduce the issue where switching between schema branches (oneOf / anyOf / if-then-else) retains stale field values in the form state.

I worked on a fix for this by implementing branch-aware cleanup inside SchemaForm. Instead of relying only on shouldUnregister, the solution explicitly unregisters fields that are no longer part of the active schema branch.

Solution

* Tracks active schema branch paths

* Automatically calls `unregister` on fields from previous branches

* Works for:
  
  * oneOf
  * anyOf
  * if/then/else
  * dependencies

Testing

* Verified via `/schema_form_demo`

* Switching branches correctly removes stale values

* No regression in existing behavior

I’d be happy to open a PR or contribute to the existing one if this approach aligns with the project direction. Please let me know your thoughts.

Hey @Jaswanth-arjun I have fixed that issue in these recent commits, thank you for reporting the issue and i have added the test for it as well. I will update the PR description, proposal and screenshot and video proof as well.

DSingh0304 avatar Mar 24 '26 12:03 DSingh0304

Hi @DSingh0304, I tested the SchemaForm demo and was able to reproduce the issue where switching between schema branches (oneOf / anyOf / if-then-else) retains stale field values in the form state. I worked on a fix for this by implementing branch-aware cleanup inside SchemaForm. Instead of relying only on shouldUnregister, the solution explicitly unregisters fields that are no longer part of the active schema branch.

Solution

* Tracks active schema branch paths

* Automatically calls `unregister` on fields from previous branches

* Works for:
  
  * oneOf
  * anyOf
  * if/then/else
  * dependencies

Testing

* Verified via `/schema_form_demo`

* Switching branches correctly removes stale values

* No regression in existing behavior

I’d be happy to open a PR or contribute to the existing one if this approach aligns with the project direction. Please let me know your thoughts.

Hey @Jaswanth-arjun I have fixed that issue in these recent commits, thank you for reporting the issue and i have added the test for it as well. I will update the PR description, proposal and screenshot and video proof as well.

Thanks for the update! @DSingh0304

I also worked on this issue and implemented a similar branch-cleanup approach with additional testing locally.

I’d be happy to help further improve this PR. Possible areas I can contribute:

  • Additional edge-case testing (nested schemas, repeated switching)
  • Performance improvements for large schemas
  • UI/UX improvements for generated forms
  • Documentation improvements for SchemaForm behavior

Please let me know if I can help with anything here.

Jaswanth-arjun avatar Mar 24 '26 12:03 Jaswanth-arjun

Hi @DSingh0304, I tested the SchemaForm demo and was able to reproduce the issue where switching between schema branches (oneOf / anyOf / if-then-else) retains stale field values in the form state. I worked on a fix for this by implementing branch-aware cleanup inside SchemaForm. Instead of relying only on shouldUnregister, the solution explicitly unregisters fields that are no longer part of the active schema branch.

Solution

* Tracks active schema branch paths

* Automatically calls `unregister` on fields from previous branches

* Works for:
  
  * oneOf
  * anyOf
  * if/then/else
  * dependencies

Testing

* Verified via `/schema_form_demo`

* Switching branches correctly removes stale values

* No regression in existing behavior

I’d be happy to open a PR or contribute to the existing one if this approach aligns with the project direction. Please let me know your thoughts.

Hey @Jaswanth-arjun I have fixed that issue in these recent commits, thank you for reporting the issue and i have added the test for it as well. I will update the PR description, proposal and screenshot and video proof as well.

Thanks for the update! @DSingh0304

I also worked on this issue and implemented a similar branch-cleanup approach with additional testing locally.

I’d be happy to help further improve this PR. Possible areas I can contribute:

* Additional edge-case testing (nested schemas, repeated switching)

* Performance improvements for large schemas

* UI/UX improvements for generated forms

* Documentation improvements for SchemaForm behavior

Please let me know if I can help with anything here.

Yes sure I will let you know if I need any help.

DSingh0304 avatar Mar 24 '26 12:03 DSingh0304

This PR has been updated and these are the updates:

1. Alignment on Correctness (Form State Cleanup) with video proof attached in description.

I have implemented a robust state management system using unregister() within the recursive SchemaField dispatcher.

  • The Fix: When a user switches branches (e.g., in a oneOf or anyOf selection), all stale/ghost values from the non-active branches are now automatically removed from the form state.
  • Submit Payload Consistency: This ensures that the final payload sent to the APISIX backend strictly adheres to the schema of the currently selected plugin variant, preventing hidden validation errors from inactive fields.

2. Verifiable Deliverables

I’ve updated my proposal to bind each phase to these deliverables:

  • Test Suite: Expanded to 47 passing tests (vitest) covering conditional branch switching and recursive validation.
  • Live Demo: Updated the schema_form_demo route with a "Live Form Data" monitor, allowing maintainers to visually verify that the payload is cleaned up in real-time as they toggle options.
  • Pass Criteria: 100% test pass rate + zero-warning pnpm lint build (verified with --max-warnings=0).

DSingh0304 avatar Mar 24 '26 12:03 DSingh0304

  • Optimized useWatch: Narrowed subscriptions in IfThenElseFields to only watch fields referenced in the if condition.

  • Recursive Cleanup: Added a collectAllPaths helper to ensure full subtree unregistration for nested objects/arrays.

  • Expanded Test Suite: Added 3 new test suites (totaling 50 tests) covering rapid branch switching, nested cleanup, and validation error purging.

DSingh0304 avatar Mar 24 '26 13:03 DSingh0304

Thank you for the significant effort on the SchemaForm component. The component architecture is well-designed with a clean recursive rendering pattern, and the test coverage is quite thorough (50 tests). The overall direction is solid.

However, there are several blocking issues that need to be resolved before merging:

  1. Dependency downgrades: This PR downgrades several TanStack packages to significantly older versions (e.g., react-router from ^1.168.3 to ^1.116.0). This is likely due to developing on an older fork of master. Please rebase onto the latest master and keep upstream dependency versions, only adding the new dependencies required by SchemaForm (ajv, vitest, testing-library, etc.).

  2. producer.ts regression: The modification to the pipeProduce function replaces produceRestoreEmptyPlugins with a two-pass marker approach, which drops the discovery_args: {} restoration logic. This change is unrelated to SchemaForm and introduces a regression. Please revert all changes to producer.ts.

  3. Unrelated .gitignore entries: The added .gemini/artifacts/ and jsonschema-poc/ entries are personal development artifacts and should not be committed to the project's .gitignore. Please remove these entries.

  4. Demo route in production: schema_form_demo is registered as a production route and will appear in release builds. Please either remove it or make it available only in development mode.

Additional suggestions (non-blocking):

  • OneOfFields and AnyOfFields are nearly identical (~150 lines of duplication). Consider extracting a shared VariantFields component.
  • findDiscriminators only recognizes const-based discriminators. Consider extending it to support single-value enum as well.
  • collectAllPaths does not handle array items or patternProperties sub-paths, which may cause missed unregistrations on branch switching.
  • formatLabel renders "oauth" as "OAUTH" while the standard form is "OAuth".

Baoyuantop avatar Apr 15 '26 02:04 Baoyuantop

Thank you for the significant effort on the SchemaForm component. The component architecture is well-designed with a clean recursive rendering pattern, and the test coverage is quite thorough (50 tests). The overall direction is solid.

However, there are several blocking issues that need to be resolved before merging:

1. Dependency downgrades: This PR downgrades several TanStack packages to significantly older versions (e.g., react-router from ^1.168.3 to ^1.116.0). This is likely due to developing on an older fork of master. Please rebase onto the latest master and keep upstream dependency versions, only adding the new dependencies required by SchemaForm (ajv, vitest, testing-library, etc.).

2. producer.ts regression: The modification to the pipeProduce function replaces produceRestoreEmptyPlugins with a two-pass marker approach, which drops the discovery_args: {} restoration logic. This change is unrelated to SchemaForm and introduces a regression. Please revert all changes to producer.ts.

3. Unrelated .gitignore entries: The added .gemini/artifacts/ and jsonschema-poc/ entries are personal development artifacts and should not be committed to the project's .gitignore. Please remove these entries.

4. Demo route in production: schema_form_demo is registered as a production route and will appear in release builds. Please either remove it or make it available only in development mode.

Additional suggestions (non-blocking):

* OneOfFields and AnyOfFields are nearly identical (~150 lines of duplication). Consider extracting a shared VariantFields component.

* findDiscriminators only recognizes const-based discriminators. Consider extending it to support single-value enum as well.

* collectAllPaths does not handle array items or patternProperties sub-paths, which may cause missed unregistrations on branch switching.

* formatLabel renders "oauth" as "OAUTH" while the standard form is "OAuth".

Thanks for the review! @Baoyuantop The blockers are fixed:

  1. Dependencies - Rebased onto latest master, all TanStack packages now aligned
  2. producer.ts - Verified discovery_args restoration is intact
  3. .gitignore - Confirmed clean, no personal artifacts tracked
  4. Demo route - Deleted schema_form_demo.tsx and demo utilities

The non-blocker issues have also been fixed.

DSingh0304 avatar Apr 15 '26 05:04 DSingh0304