Fix websocket import in mains plugin and ArrayOf type assertion in testing plugin#235
Merged
Fix websocket import in mains plugin and ArrayOf type assertion in testing plugin#235
Conversation
This change enhances the mains plugin to support gRPC servers in addition to HTTP servers, addressing issue #209. Key changes: - Only generate HTTP server code when the service design includes HTTP endpoints - Only generate gRPC server code when the service design includes gRPC endpoints - Always generate the metrics HTTP server (health/metrics/debug endpoints) - Fix duplicate package/import declarations in generated main.go - Fix WebSocket detection bug (Stream value of 0 means no streaming, not NoStreamKind) - Conditionally import transport-specific packages based on what's actually used - Add proper OTel instrumentation for gRPC servers - Implement graceful shutdown for both HTTP and gRPC servers The plugin now: 1. Scans the DSL for HTTP and gRPC endpoint definitions 2. Conditionally generates server initialization code based on transports used 3. Manages imports efficiently (only includes websocket, grpc packages when needed) 4. Provides a unified main.go that can run HTTP-only, gRPC-only, or both transports 5. Maintains the existing single-service (services/<svc>/cmd/<svc>) and multi-service (cmd/<server>) layouts Fixes #209
The testing plugin was generating invalid Go for array results in
ScenarioRunner.callValidator, producing code like:
typedResult := result.(*bff.[]*AccessControl)
instead of the correct:
typedResult := result.([]*bff.AccessControl)
This was caused by hardcoding the assertion shape as
`result.(*{{Pkg}}.{{Result}})`, which breaks for composite types.
Fix: Use Goa's GoFullTypeRef to compute the properly qualified type
reference (handling *, [], map[...], etc.) and store it in a new
ResultTypeRef field on scenarioMethodData.
Added regression test to ensure this pattern never recurs.
The mains plugin was using httpcodegen.NeedDialer() to detect websocket usage, but that function is designed for client dialers, not servers. This caused the github.com/gorilla/websocket import to be missing when websocket.Upgrader was emitted in the generated main. Fix: Use httpWebSocketByService() to detect streaming endpoints that require websocket support, matching the same detection used for HasAnyWebSocket in the template. Also fixed the test to check the websocket import in the correct section (source-header) rather than the main body section.
Goa v3.23.4 renamed Schema.Definitions to Schema.Defs to align with JSON Schema draft 2020-12 which uses $defs instead of definitions. Changes: - Update dupSchema() to use Defs field instead of Definitions - Update inlineRefs() to handle both #/$defs/ and #/definitions/ prefixes - Update go.mod to require goa v3.23.4 - Regenerate golden files with new $ref format
Resolve conflicts by: - Taking origin/v3 file server support (FSCounts, FileServerNils) - Keeping websocket detection fix using httpWebSocketByService() - Keeping test fix to check import in source-header section
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR contains two bug fixes:
1. mains: Fix websocket import detection for server mains
The mains plugin was using
httpcodegen.NeedDialer()to detect websocket usage, but that function is designed for client dialers, not servers. This caused thegithub.com/gorilla/websocketimport to be missing whenwebsocket.Upgraderwas emitted in the generated main.Fix: Use
httpWebSocketByService()to detect streaming endpoints that require websocket support, matching the same detection used forHasAnyWebSocketin the template.2. testing: Fix type assertion for ArrayOf results in ScenarioRunner (#234)
The testing plugin was generating invalid Go for array results in
ScenarioRunner.callValidator, producing code like:instead of the correct:
This was caused by hardcoding the assertion shape as
result.(*{{Pkg}}.{{Result}}), which breaks for composite types likeArrayOf(...).Fix: Use Goa's
GoFullTypeRefto compute the properly qualified type reference (handling*,[],map[...], etc.) and store it in a newResultTypeReffield onscenarioMethodData.Testing
go test ./...)TestGenerateScenarios_ArrayResultTypeAssertionto ensure the ArrayOf type assertion fix never regressesTestWebSocketMainIncludesUpgraderto check the correct section for the websocket importFixes #234