Skip to content

Add JSON-RPC 2.0 Transport Support to Goa #3734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open

Add JSON-RPC 2.0 Transport Support to Goa #3734

wants to merge 1 commit into from

Conversation

raphael
Copy link
Member

@raphael raphael commented Jun 30, 2025

This PR introduces JSON-RPC 2.0 protocol support to the Goa framework, enabling developers to build JSON-RPC APIs alongside HTTP REST and gRPC using the same DSL.

Features

Core JSON-RPC Support

  • Full JSON-RPC 2.0 Compliance: Implements the complete JSON-RPC 2.0 specification including request/response format, error codes, and batch requests
  • Multiple Transport Options:
    • HTTP POST for standard RPC calls
    • WebSocket for bidirectional streaming
    • Server-Sent Events (SSE) for server-side streaming
  • Unified DSL: Use the same Goa DSL to define services that can be exposed via REST, gRPC, and JSON-RPC

Advanced Features

  • Batch request processing: The generated servers and clients support single elements or arrays of elements in request params
  • Request ID correlation: The generated code takes care of populating the request ID in responses

Code Generation

  • Server Generation: Complete server implementation with routing, request decoding, and response encoding
  • Client Generation: Type-safe client with automatic request/response handling
  • Streaming Support: Generated code for WebSocket and SSE streaming endpoints
  • Example Generation: Working server and client examples via goa example

DSL Integration

  Service("Calculator", func() {
      Method("add", func() {
          Payload(func() {
              Attribute("a", Int)
              Attribute("b", Int)
          })
          Result(Int)

          // JSON-RPC transport configuration
          JSONRPC(func() {
              POST("/rpc")  // HTTP endpoint
              MethodName("Calculator.Add")  // Custom method name
          })
      })
  })

Method Routing

JSON-RPC transport brings a key architectural difference: multiple methods can share the same HTTP endpoint, with method routing handled by the JSON-RPC method field rather than URL paths. This allows for cleaner API organization where related methods can be grouped under a single endpoint.

Service("Calculator", func() {
    Method("add", func() {
        JSONRPC(func() {
            POST("/api/v1")  // Same endpoint as subtract
        })
    })

    Method("subtract", func() {
        JSONRPC(func() {
            POST("/api/v1")  // Same endpoint as add
        })
    })

    Method("multiply", func() {
        JSONRPC(func() {
            POST("/api/v2")  // Different version endpoint
        })
    })
})

Testing

Comprehensive test coverage including protocol compliance tests, integration tests, and golden file tests for generated code. All tests pass with make.

- Added new templates for handling various code generation scenarios, including validation and transformation for different data types.
- Refactored existing templates to improve readability and maintainability.
- Updated the .golangci.yml configuration to include specific checks for static analysis.
- Bumped dependencies in go.mod and updated go.sum accordingly.
- Removed obsolete staticcheck configuration file.

This commit enhances the code generation process and ensures better compliance with coding standards.
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.

1 participant