Skip to content

build(gradle): add build-time validation for prefabs and JSON assets #4986#5326

Open
mmruii wants to merge 2 commits into
MovingBlocks:developfrom
mmruii:build/4986-json-validation
Open

build(gradle): add build-time validation for prefabs and JSON assets #4986#5326
mmruii wants to merge 2 commits into
MovingBlocks:developfrom
mmruii:build/4986-json-validation

Conversation

@mmruii
Copy link
Copy Markdown

@mmruii mmruii commented Apr 20, 2026

This PR implements build-time validation for all JSON-based assets (such as .prefab, .block, .ui, and .json files) in both engine and modules.
It introduces a custom Gradle task that parses each asset and fails the build with a descriptive error if any file contains malformed JSON.

Modified files:

ValidateJsonAssets.kt:
New Gradle task that validates JSON assets at build time.
build.gradle.kts:
Adds the org.json dependency and test dependencies for the new task.
terasology-module.gradle.kts:
Registers the validation task for all modules and hooks it into the build process.
build.gradle.kts:
Registers the validation task for engine assets and hooks it into the build process.
build-logic/src/test/kotlin/org/terasology/gradology/ValidateJsonAssetsTest.kt:
Unit tests for the validation task, covering valid, invalid, and missing asset scenarios.
How to test:

Run a build (./gradlew build) with valid and invalid JSON assets to confirm that the build fails on malformed files and succeeds otherwise.
Run the included unit tests for the validation task.
Outstanding before merging:

Review if additional asset types should be included.
Documentation update if needed.

Closes #4986

mmruii added 2 commits April 20, 2026 10:11
Registers a ValidateJsonAssets Gradle task that parses all JSON-based
asset files (*.prefab, *.block, *.ui, *.json) under the assets directory
at build time, failing the build with a descriptive message if any file
contains malformed JSON.

- Add ValidateJsonAssets task class to build-logic
- Add org.json:json dependency to build-logic for JSON parsing
- Hook validateJsonAssets into processResources for modules
- Hook validateJsonAssets into processResources for engine

Closes MovingBlocks#4986
Tests cover:
- Task registration on a Gradle project
- Valid JSON/prefab files pass without errors
- Malformed JSON files fail the build with a descriptive message
  mentioning the offending file
- Projects with no assets directory succeed gracefully

Also refactor ValidateJsonAssets to use ConfigurableFileCollection
with @SkipWhenEmpty for proper Gradle up-to-date checking, and
replace deprecated createTempDir() with Files.createTempDirectory().
@Cervator
Copy link
Copy Markdown
Member

Hi there, welcome, and thank you 👍

It looks like you already triggered a good looking test in the Jenkins build :-)

* What went wrong:
Execution failed for task ':engine:validateJsonAssets'.
> Found 1 invalid JSON asset(s):
    - /home/jenkins/agent/workspace/Terasology_engine_PR-5326/engine/src/main/resources/org/terasology/engine/assets/ui/menu/moduleDetailsScreen.ui: Duplicate key "layoutInfo" at 20601 [character 37 line 333]

That's encouraging! Although it does need a quick fix in that actual file - great example though 😁

And I see tests, also good!

I'll try to find time to test and review a bit later, but one thing that stuck out is implementation("org.json:json:20240303") - it could just be a copy pasted snippet, I find that using AI tools tends to arbitrarily pick a version unless you make it go look for the latest. It does appear like there are newer versions of that library, so we should probably aim for the latest (just a few months old)

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.

add build-time validation for prefabs and other JSON assets

2 participants