feat(java): support transforming modular models#615
Conversation
Adds modular-model support to the Java package, matching the Go implementation: - ModulesToModelTransformer merges a set of module files into a single AuthorizationModel, tagging type/relation/condition metadata with module and source-info, handling `extend type`, and reporting transformation and syntax errors (duplicate type/condition, extending a missing type, redefining a relation, non-module files). - The DSL listener now records the module name and stamps module metadata on types, conditions, and extension relations; the modular parse result exposes type-definition extensions. - JsonToDslTransformer gains an includeSourceInformation option that emits `# module: x, file: y` comments (and `# extended by:` for extension relations) with module-ordered output, plus getModulesFromJSON returning the sorted module list. - ModelUtils.isModelModular mirrors the JS helper. Test runners drive the shared tests/data/transformer-module fixtures. As in the Go runner, model-validation errors are filtered out and validation-only cases are skipped, since the transformer surfaces transformation/syntax errors only. Also documents the modules-to-model transform in the Java README and applies Spotless formatting.
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Adds modular-model support to the Java package of OpenFGA Language, aligning Java’s DSL↔JSON tooling and tests with the existing modular DSL behavior and shared fixtures.
Changes:
- Introduces
ModulesToModelTransformerplus supportingModuleFile/ModuleDslto merge multiple module DSL files into a singleAuthorizationModelwith source-info. - Extends parsing + transformation to carry module metadata (types/conditions/extension relations) and to optionally emit module/file source comments when rendering DSL from JSON.
- Adds Java test harness for
tests/data/transformer-modulefixtures and updates documentation.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/java/src/test/java/dev/openfga/language/utils/ModelUtilsTest.java | Adds unit tests for ModelUtils.isModelModular. |
| pkg/java/src/test/java/dev/openfga/language/util/TestsData.java | Loads shared transformer-module fixtures and module files for parameterized tests. |
| pkg/java/src/test/java/dev/openfga/language/util/ModuleTransformerTestCase.java | Test case holder for module transformer fixtures. |
| pkg/java/src/test/java/dev/openfga/language/util/ModuleExpectedError.java | Fixture error DTO + filtering helper for validation-only errors. |
| pkg/java/src/test/java/dev/openfga/language/ModuleToModelShould.java | Runs module→model transform tests over shared fixtures. |
| pkg/java/src/test/java/dev/openfga/language/JsonToDslShould.java | Adds modular JSON→DSL + module extraction tests over shared fixtures. |
| pkg/java/src/main/java/dev/openfga/language/utils/ModelUtils.java | Adds isModelModular helper for detecting modular models. |
| pkg/java/src/main/java/dev/openfga/language/OpenFgaDslListener.java | Captures module name and stamps module metadata; exposes type extensions. |
| pkg/java/src/main/java/dev/openfga/language/ModulesToModelTransformer.java | New transformer to merge module files and report transformation/syntax errors. |
| pkg/java/src/main/java/dev/openfga/language/ModuleFile.java | New module-file value object (name + DSL contents). |
| pkg/java/src/main/java/dev/openfga/language/ModuleDsl.java | New helper for resolving line/column for module error reporting. |
| pkg/java/src/main/java/dev/openfga/language/JsonToDslTransformer.java | Adds includeSourceInformation, modular ordering, and module list extraction. |
| pkg/java/src/main/java/dev/openfga/language/errors/UnsupportedModularModulesException.java | New exception for module operations on unsupported schema versions. |
| pkg/java/src/main/java/dev/openfga/language/errors/ParsingError.java | Adds optional file field for error serialization/deserialization. |
| pkg/java/src/main/java/dev/openfga/language/errors/ModuleTransformationSingleError.java | New per-error wrapper for module transformation errors. |
| pkg/java/src/main/java/dev/openfga/language/errors/ModuleTransformationError.java | New aggregate exception for module transformation errors. |
| pkg/java/src/main/java/dev/openfga/language/DslToJsonTransformer.java | Exposes modular parse result (extensions) and reuses a shared walk helper. |
| pkg/java/README.md | Documents Java “Modules To Model” transformation usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var extensions = result.getTypeDefExtensions(); | ||
| for (var typeDef : result.getAuthorizationModel().getTypeDefinitions()) { |
| private static boolean isModularModel(AuthorizationModel model) { | ||
| if (model == null || model.getTypeDefinitions() == null) { | ||
| return false; | ||
| } | ||
| return model.getTypeDefinitions().stream().anyMatch(typeDef -> !isBlank(module(typeDef.getMetadata()))); | ||
| } |
| for (var relationName : relationNames) { | ||
| var formattedRelationString = formatRelation( | ||
| typeName, relationName, relations.get(relationName), metadataRelations.get(relationName)); | ||
| formatedTypeBuilder.append(EOL).append(formattedRelationString); | ||
| } |
| public static boolean isModelModular(AuthorizationModel model) { | ||
| var schemaVersion = model != null && model.getSchemaVersion() != null ? model.getSchemaVersion() : "1.1"; | ||
| switch (schemaVersion) { | ||
| case "1.0": | ||
| case "1.1": | ||
| return false; | ||
| case "1.2": | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported schema version: " + schemaVersion); | ||
| } | ||
|
|
||
| if (model.getTypeDefinitions() == null) { | ||
| return false; | ||
| } |
Adds modular-model support to the Java package, matching the Go implementation:
extend type, and reporting transformation and syntax errors (duplicate type/condition, extending a missing type, redefining a relation, non-module files).# module: x, file: ycomments (and# extended by:for extension relations) with module-ordered output, plus getModulesFromJSON returning the sorted module list.Test runners drive the shared tests/data/transformer-module fixtures. As in the Go runner, model-validation errors are filtered out and validation-only cases are skipped, since the transformer surfaces transformation/syntax errors only.
Also documents the modules-to-model transform in the Java README and applies Spotless formatting.
Description
What problem is being solved?
How is it being solved?
What changes are made to solve it?
References
Review Checklist
main