Skip to content

fix(go): handle proto direct assignments in DSL transform#621

Open
ErlingWis wants to merge 1 commit into
openfga:mainfrom
ErlingWis:fix-proto-json
Open

fix(go): handle proto direct assignments in DSL transform#621
ErlingWis wants to merge 1 commit into
openfga:mainfrom
ErlingWis:fix-proto-json

Conversation

@ErlingWis

@ErlingWis ErlingWis commented Jun 26, 2026

Copy link
Copy Markdown

Description

Fixes TransformJSONProtoToDSL for in-memory authorization models produced by TransformModuleFilesToModel when relations contain direct
assignments.

What problem is being solved?

Direct assignments are represented by the Userset_This protobuf oneof case. The previous implementation detected direct assignments with
GetThis() != nil, which can fail when the oneof case is set but the inner DirectUserset sentinel is nil.

This caused valid module models to fail when round-tripping through:

TransformModuleFilesToModel -> TransformJSONProtoToDSL

How is it being solved?

TransformJSONProtoToDSL now detects direct assignments by checking whether the userset is the Userset_This oneof case, instead of
requiring GetThis() to return a populated DirectUserset.

The DSL-to-proto transformer also now populates DirectUserset when creating direct-assignment rewrites.

What changes are made to solve it?

  • Detect direct assignments by checking the Userset_This oneof case.
  • Populate DirectUserset when transforming DSL direct assignments to proto.
  • Add module round-trip test coverage for TransformJSONProtoToDSL, with and without source information.

References

closes #620

Review Checklist

  • [x ] I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • [ x] I have added tests to validate that the change in functionality is working as expected

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of direct assignments when converting between JSON/protobuf and DSL, making rewritten output more consistent and reliable.
    • Fixed direct-assignment detection in nested relation cases so conversion results are handled correctly across different rewrite paths.
  • Tests

    • Added coverage for JSON-to-DSL conversion, including output with and without source information.

Copilot AI review requested due to automatic review settings June 26, 2026 22:09
@ErlingWis ErlingWis requested review from a team as code owners June 26, 2026 22:09
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 26, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: ErlingWis / name: Erling Wisløff (7f33cfc)

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 08e04898-9d2d-4517-8fdb-be48d880fe3f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

Direct-assignment usersets are now emitted with an explicit DirectUserset payload, and the JSON-to-DSL transformer recognizes direct assignments by concrete protobuf type across validation, parsing, and prioritization. A new test covers module-to-DSL transforms with and without source information.

Changes

Direct assignment round-trip

Layer / File(s) Summary
Emit and detect direct assignment
pkg/go/transformer/dsltojson.go, pkg/go/transformer/jsontodsl.go
TransformJSONProtoToDSL now emits direct assignments with an explicit DirectUserset, and JSON-to-DSL direct-assignment handling uses concrete Userset_This checks in validation, parsing, and prioritization.
Module-to-DSL coverage
pkg/go/transformer/jsontodsl_test.go
A new test transforms module cases to a model, runs JSON-to-DSL with and without source information, and compares both DSL outputs to expected fixtures.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: handling proto direct assignments in DSL transforms.
Linked Issues check ✅ Passed The changes address #620 by fixing direct-assignment detection and adding round-trip coverage for TransformJSONProtoToDSL.
Out of Scope Changes check ✅ Passed The added test coverage and transformer adjustments stay within the direct-assignment DSL transform fix scope.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the Go TransformJSONProtoToDSL transformer to correctly recognize direct assignments when the protobuf Userset oneof is set to Userset_This but the inner DirectUserset message is nil (a case that can occur for in-memory models produced by TransformModuleFilesToModel). It also ensures the DSL→proto transformer always populates the DirectUserset sentinel for direct-assignment rewrites, and adds round-trip coverage for modular models.

Changes:

  • Detect direct assignments by checking the Userset_This oneof case (instead of GetThis() != nil).
  • Populate DirectUserset when creating direct-assignment rewrites in the DSL→proto transformer.
  • Add module round-trip tests for TransformJSONProtoToDSL with and without source information.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/go/transformer/jsontodsl.go Adds isDirectAssignment and uses it throughout parsing/validation to correctly detect Userset_This even when DirectUserset is nil.
pkg/go/transformer/dsltojson.go Ensures direct-assignment rewrites explicitly set Userset_This.This to an empty DirectUserset{} sentinel.
pkg/go/transformer/jsontodsl_test.go Adds modular-model round-trip tests for TransformJSONProtoToDSL (with/without source info).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Detect direct assignments by the Userset_This oneof case instead of requiring GetThis to return a populated DirectUserset.

Populate DirectUserset when transforming DSL to proto and cover module model round trips through TransformJSONProtoToDSL.
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.

TransformJSONProtoToDSL fails for models with direct assignments

2 participants