feat(cmd/api): expose runtime spec metadata flags
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
006a065ea0
commit
f0b2d8b248
3 changed files with 94 additions and 2 deletions
|
|
@ -33,6 +33,9 @@ func addSDKCommand(parent *cli.Command) {
|
|||
version string
|
||||
graphqlPath string
|
||||
ssePath string
|
||||
wsPath string
|
||||
pprofEnabled bool
|
||||
expvarEnabled bool
|
||||
termsURL string
|
||||
contactName string
|
||||
contactURL string
|
||||
|
|
@ -52,7 +55,7 @@ func addSDKCommand(parent *cli.Command) {
|
|||
|
||||
// If no spec file provided, generate one to a temp file.
|
||||
if specFile == "" {
|
||||
builder := sdkSpecBuilder(title, description, version, graphqlPath, ssePath, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers)
|
||||
builder := sdkSpecBuilder(title, description, version, graphqlPath, ssePath, wsPath, pprofEnabled, expvarEnabled, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers)
|
||||
groups := sdkSpecGroupsIter()
|
||||
|
||||
tmpFile, err := os.CreateTemp("", "openapi-*.json")
|
||||
|
|
@ -103,6 +106,9 @@ func addSDKCommand(parent *cli.Command) {
|
|||
cli.StringFlag(cmd, &version, "version", "V", defaultSDKVersion, "API version in generated spec")
|
||||
cli.StringFlag(cmd, &graphqlPath, "graphql-path", "", "", "GraphQL endpoint path in generated spec")
|
||||
cli.StringFlag(cmd, &ssePath, "sse-path", "", "", "SSE endpoint path in generated spec")
|
||||
cli.StringFlag(cmd, &wsPath, "ws-path", "", "", "WebSocket endpoint path in generated spec")
|
||||
cli.BoolFlag(cmd, &pprofEnabled, "pprof", "", false, "Include pprof endpoints in generated spec")
|
||||
cli.BoolFlag(cmd, &expvarEnabled, "expvar", "", false, "Include expvar endpoint in generated spec")
|
||||
cli.StringFlag(cmd, &termsURL, "terms-of-service", "", "", "OpenAPI terms of service URL in generated spec")
|
||||
cli.StringFlag(cmd, &contactName, "contact-name", "", "", "OpenAPI contact name in generated spec")
|
||||
cli.StringFlag(cmd, &contactURL, "contact-url", "", "", "OpenAPI contact URL in generated spec")
|
||||
|
|
@ -116,13 +122,16 @@ func addSDKCommand(parent *cli.Command) {
|
|||
parent.AddCommand(cmd)
|
||||
}
|
||||
|
||||
func sdkSpecBuilder(title, description, version, graphqlPath, ssePath, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers string) *goapi.SpecBuilder {
|
||||
func sdkSpecBuilder(title, description, version, graphqlPath, ssePath, wsPath string, pprofEnabled, expvarEnabled bool, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers string) *goapi.SpecBuilder {
|
||||
return &goapi.SpecBuilder{
|
||||
Title: title,
|
||||
Description: description,
|
||||
Version: version,
|
||||
GraphQLPath: graphqlPath,
|
||||
SSEPath: ssePath,
|
||||
WSPath: wsPath,
|
||||
PprofEnabled: pprofEnabled,
|
||||
ExpvarEnabled: expvarEnabled,
|
||||
TermsOfService: termsURL,
|
||||
ContactName: contactName,
|
||||
ContactURL: contactURL,
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@ func addSpecCommand(parent *cli.Command) {
|
|||
version string
|
||||
graphqlPath string
|
||||
ssePath string
|
||||
wsPath string
|
||||
pprofEnabled bool
|
||||
expvarEnabled bool
|
||||
termsURL string
|
||||
contactName string
|
||||
contactURL string
|
||||
|
|
@ -39,6 +42,9 @@ func addSpecCommand(parent *cli.Command) {
|
|||
Version: version,
|
||||
GraphQLPath: graphqlPath,
|
||||
SSEPath: ssePath,
|
||||
WSPath: wsPath,
|
||||
PprofEnabled: pprofEnabled,
|
||||
ExpvarEnabled: expvarEnabled,
|
||||
TermsOfService: termsURL,
|
||||
ContactName: contactName,
|
||||
ContactURL: contactURL,
|
||||
|
|
@ -71,6 +77,9 @@ func addSpecCommand(parent *cli.Command) {
|
|||
cli.StringFlag(cmd, &version, "version", "V", "1.0.0", "API version in spec")
|
||||
cli.StringFlag(cmd, &graphqlPath, "graphql-path", "", "", "GraphQL endpoint path in generated spec")
|
||||
cli.StringFlag(cmd, &ssePath, "sse-path", "", "", "SSE endpoint path in generated spec")
|
||||
cli.StringFlag(cmd, &wsPath, "ws-path", "", "", "WebSocket endpoint path in generated spec")
|
||||
cli.BoolFlag(cmd, &pprofEnabled, "pprof", "", false, "Include pprof endpoints in generated spec")
|
||||
cli.BoolFlag(cmd, &expvarEnabled, "expvar", "", false, "Include expvar endpoint in generated spec")
|
||||
cli.StringFlag(cmd, &termsURL, "terms-of-service", "", "", "OpenAPI terms of service URL in spec")
|
||||
cli.StringFlag(cmd, &contactName, "contact-name", "", "", "OpenAPI contact name in spec")
|
||||
cli.StringFlag(cmd, &contactURL, "contact-url", "", "", "OpenAPI contact URL in spec")
|
||||
|
|
|
|||
|
|
@ -88,6 +88,15 @@ func TestAPISpecCmd_Good_JSON(t *testing.T) {
|
|||
if specCmd.Flag("sse-path") == nil {
|
||||
t.Fatal("expected --sse-path flag on spec command")
|
||||
}
|
||||
if specCmd.Flag("ws-path") == nil {
|
||||
t.Fatal("expected --ws-path flag on spec command")
|
||||
}
|
||||
if specCmd.Flag("pprof") == nil {
|
||||
t.Fatal("expected --pprof flag on spec command")
|
||||
}
|
||||
if specCmd.Flag("expvar") == nil {
|
||||
t.Fatal("expected --expvar flag on spec command")
|
||||
}
|
||||
if specCmd.Flag("terms-of-service") == nil {
|
||||
t.Fatal("expected --terms-of-service flag on spec command")
|
||||
}
|
||||
|
|
@ -480,6 +489,50 @@ func TestAPISpecCmd_Good_SSEPathPopulatesSpec(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAPISpecCmd_Good_RuntimePathsPopulatedSpec(t *testing.T) {
|
||||
root := &cli.Command{Use: "root"}
|
||||
AddAPICommands(root)
|
||||
|
||||
outputFile := t.TempDir() + "/spec.json"
|
||||
root.SetArgs([]string{
|
||||
"api", "spec",
|
||||
"--ws-path", "/ws",
|
||||
"--pprof",
|
||||
"--expvar",
|
||||
"--output", outputFile,
|
||||
})
|
||||
root.SetErr(new(bytes.Buffer))
|
||||
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(outputFile)
|
||||
if err != nil {
|
||||
t.Fatalf("expected spec file to be written: %v", err)
|
||||
}
|
||||
|
||||
var spec map[string]any
|
||||
if err := json.Unmarshal(data, &spec); err != nil {
|
||||
t.Fatalf("expected valid JSON spec, got error: %v", err)
|
||||
}
|
||||
|
||||
paths, ok := spec["paths"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected paths object in generated spec, got %T", spec["paths"])
|
||||
}
|
||||
|
||||
if _, ok := paths["/ws"]; !ok {
|
||||
t.Fatal("expected WebSocket path to be included in generated spec")
|
||||
}
|
||||
if _, ok := paths["/debug/pprof"]; !ok {
|
||||
t.Fatal("expected pprof path to be included in generated spec")
|
||||
}
|
||||
if _, ok := paths["/debug/vars"]; !ok {
|
||||
t.Fatal("expected expvar path to be included in generated spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPISDKCmd_Bad_EmptyLanguages(t *testing.T) {
|
||||
root := &cli.Command{Use: "root"}
|
||||
AddAPICommands(root)
|
||||
|
|
@ -552,6 +605,15 @@ func TestAPISDKCmd_Good_ValidatesLanguage(t *testing.T) {
|
|||
if sdkCmd.Flag("sse-path") == nil {
|
||||
t.Fatal("expected --sse-path flag on sdk command")
|
||||
}
|
||||
if sdkCmd.Flag("ws-path") == nil {
|
||||
t.Fatal("expected --ws-path flag on sdk command")
|
||||
}
|
||||
if sdkCmd.Flag("pprof") == nil {
|
||||
t.Fatal("expected --pprof flag on sdk command")
|
||||
}
|
||||
if sdkCmd.Flag("expvar") == nil {
|
||||
t.Fatal("expected --expvar flag on sdk command")
|
||||
}
|
||||
if sdkCmd.Flag("terms-of-service") == nil {
|
||||
t.Fatal("expected --terms-of-service flag on sdk command")
|
||||
}
|
||||
|
|
@ -591,6 +653,9 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) {
|
|||
"9.9.9",
|
||||
"/gql",
|
||||
"/events",
|
||||
"/ws",
|
||||
true,
|
||||
true,
|
||||
"https://example.com/terms",
|
||||
"SDK Support",
|
||||
"https://example.com/support",
|
||||
|
|
@ -642,6 +707,15 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) {
|
|||
if _, ok := paths["/events"]; !ok {
|
||||
t.Fatal("expected SSE path to be included in generated spec")
|
||||
}
|
||||
if _, ok := paths["/ws"]; !ok {
|
||||
t.Fatal("expected WebSocket path to be included in generated spec")
|
||||
}
|
||||
if _, ok := paths["/debug/pprof"]; !ok {
|
||||
t.Fatal("expected pprof path to be included in generated spec")
|
||||
}
|
||||
if _, ok := paths["/debug/vars"]; !ok {
|
||||
t.Fatal("expected expvar path to be included in generated spec")
|
||||
}
|
||||
|
||||
if info["termsOfService"] != "https://example.com/terms" {
|
||||
t.Fatalf("expected termsOfService to be preserved, got %v", info["termsOfService"])
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue