Skip to content

Commit 11653fd

Browse files
authored
docs: enhance plugin generation and expression handling (#217)
* docs: enhance plugin generation and expression handling - Improve docs plugin generation logic with better error handling - Add new expression types and root handling in docs/expr - Update DSL definitions for enhanced documentation features - Update dependencies in go.mod and go.sum This commit enhances the documentation plugin with improved generation capabilities and better expression handling for more robust documentation generation. * fix: remove unreachable case clause in walkDeps Remove the unreachable *goaexpr.ResultTypeExpr case since ResultTypeExpr implements UserType interface and is already handled by the goaexpr.UserType case. This fixes the static analysis warning SA4020.
1 parent ac97c49 commit 11653fd

5 files changed

Lines changed: 203 additions & 82 deletions

File tree

docs/dsl/docs.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,58 @@ import (
44
"goa.design/goa/v3/eval"
55
goaexpr "goa.design/goa/v3/expr"
66
"goa.design/plugins/v3/docs"
7-
"goa.design/plugins/v3/docs/expr"
7+
plugexpr "goa.design/plugins/v3/docs/expr"
88
)
99

1010
// init registers the docs plugin when importing the DSL.
1111
func init() { docs.Register() }
1212

1313
// UseJSONTags configures the docs plugin to use JSON struct tags declared via
14-
// Meta("struct:tag:json", ...) as field names in generated docs. This setting
15-
// affects definitions, payloads, results, and error schemas and examples.
16-
func UseJSONTags() { expr.Root.UseJSONTags = true }
14+
// Meta("struct:tag:json", ...) as field names in generated docs.
15+
//
16+
// This setting affects type definitions, payloads, results, and error schemas
17+
// and their examples.
18+
//
19+
// Usage:
20+
//
21+
// var _ = API("my api", func() {
22+
// // Use field names from struct JSON tags throughout docs.json
23+
// docs.UseJSONTags()
24+
// })
25+
func UseJSONTags() { plugexpr.Root.UseJSONTags = true }
1726

1827
// InlineRefs configures the docs plugin to inline referenced schemas in JSON
19-
// Schema output where possible. Cycles are preserved by keeping $ref.
20-
func InlineRefs() { expr.Root.InlineRefs = true }
28+
// Schema output where possible. Cycles are preserved by keeping $ref where
29+
// needed (e.g., recursive references).
30+
//
31+
// Usage:
32+
//
33+
// var _ = API("my api", func() {
34+
// // Prefer inlined schemas rather than $ref when there are no cycles
35+
// docs.InlineRefs()
36+
// })
37+
func InlineRefs() { plugexpr.Root.InlineRefs = true }
38+
39+
// IncludeTypes registers user types to force-emit in docs.json definitions.
40+
// Accepts user types defined via Type or ResultType.
41+
//
42+
// Service and method schemas are always generated by the docs plugin when not
43+
// disabled on the service/method. IncludeTypes does not change service/method
44+
// emission. This is only useful for types that are generated via the "force"
45+
// Goa Meta DSL; it ensures those user types (and their transitive dependencies)
46+
// are included under the top-level "definitions" object.
47+
//
48+
// Usage:
49+
//
50+
// var _ = API("my api", func() {
51+
// // Ensure these shared types appear in docs.json definitions so downstream
52+
// // tooling can resolve schemas by name.
53+
// docs.IncludeTypes(MySharedType, AnotherType)
54+
// })
55+
func IncludeTypes(ts ...goaexpr.UserType) { plugexpr.Root.IncludeTypes(ts...) }
2156

22-
// DisableDocs disables docs.json generation for the current service only.
23-
// It does not affect OpenAPI generation. Invoke inside a Service DSL.
57+
// DisableDocs disables docs.json generation for the current service or method.
58+
// It does not affect OpenAPI generation. Invoke inside a Service or Method DSL.
2459
//
2560
// Service("front", func() {
2661
// DisableDocs()

docs/expr/root.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type (
1818
// replacing them with copies of their referenced definitions where
1919
// possible. Cycles are preserved by leaving $ref in place when needed.
2020
InlineRefs bool
21+
22+
// IncludedTypes are the user types to force-emit into definitions.
23+
IncludedTypes []expr.UserType
2124
}
2225
)
2326

@@ -39,3 +42,12 @@ func (*RootExpr) DependsOn() []eval.Root { return []eval.Root{expr.Root} }
3942
// This is used to skip frames that point to files in these packages when
4043
// computing the location of errors.
4144
func (*RootExpr) Packages() []string { return []string{"goa.design/plugins/v3/docs/dsl"} }
45+
46+
// IncludeTypes registers the given user types for forced definition emission.
47+
func (r *RootExpr) IncludeTypes(ts ...expr.UserType) {
48+
for _, t := range ts {
49+
if t != nil {
50+
r.IncludedTypes = append(r.IncludedTypes, t)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)