diff --git a/cmd/api/cmd_sdk.go b/cmd/api/cmd_sdk.go index 806f026..b3f3e25 100644 --- a/cmd/api/cmd_sdk.go +++ b/cmd/api/cmd_sdk.go @@ -33,6 +33,7 @@ func addSDKCommand(parent *cli.Command) { description string version string graphqlPath string + ssePath string termsURL string contactName string contactURL string @@ -52,7 +53,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, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers) + builder := sdkSpecBuilder(title, description, version, graphqlPath, ssePath, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers) groups := sdkSpecGroupsIter() tmpFile, err := os.CreateTemp("", "openapi-*.json") @@ -102,6 +103,7 @@ func addSDKCommand(parent *cli.Command) { cli.StringFlag(cmd, &description, "description", "d", defaultSDKDescription, "API description in generated spec") 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, &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") @@ -115,12 +117,13 @@ func addSDKCommand(parent *cli.Command) { parent.AddCommand(cmd) } -func sdkSpecBuilder(title, description, version, graphqlPath, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers string) *goapi.SpecBuilder { +func sdkSpecBuilder(title, description, version, graphqlPath, ssePath, 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, TermsOfService: termsURL, ContactName: contactName, ContactURL: contactURL, diff --git a/cmd/api/cmd_spec.go b/cmd/api/cmd_spec.go index bd777e1..dbf5c98 100644 --- a/cmd/api/cmd_spec.go +++ b/cmd/api/cmd_spec.go @@ -19,6 +19,7 @@ func addSpecCommand(parent *cli.Command) { description string version string graphqlPath string + ssePath string termsURL string contactName string contactURL string @@ -37,6 +38,7 @@ func addSpecCommand(parent *cli.Command) { Description: description, Version: version, GraphQLPath: graphqlPath, + SSEPath: ssePath, TermsOfService: termsURL, ContactName: contactName, ContactURL: contactURL, @@ -68,6 +70,7 @@ func addSpecCommand(parent *cli.Command) { cli.StringFlag(cmd, &description, "description", "d", "Lethean Core API", "API description in spec") 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, &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") diff --git a/cmd/api/cmd_test.go b/cmd/api/cmd_test.go index 4f54b47..97a3a9a 100644 --- a/cmd/api/cmd_test.go +++ b/cmd/api/cmd_test.go @@ -85,6 +85,9 @@ func TestAPISpecCmd_Good_JSON(t *testing.T) { if specCmd.Flag("graphql-path") == nil { t.Fatal("expected --graphql-path flag on spec command") } + if specCmd.Flag("sse-path") == nil { + t.Fatal("expected --sse-path flag on spec command") + } if specCmd.Flag("terms-of-service") == nil { t.Fatal("expected --terms-of-service flag on spec command") } @@ -441,6 +444,42 @@ func TestAPISpecCmd_Good_GraphQLPathPopulatesSpec(t *testing.T) { } } +func TestAPISpecCmd_Good_SSEPathPopulatesSpec(t *testing.T) { + root := &cli.Command{Use: "root"} + AddAPICommands(root) + + outputFile := t.TempDir() + "/spec.json" + root.SetArgs([]string{ + "api", "spec", + "--sse-path", "/events", + "--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["/events"]; !ok { + t.Fatal("expected SSE path to be included in generated spec") + } +} + func TestAPISDKCmd_Bad_EmptyLanguages(t *testing.T) { root := &cli.Command{Use: "root"} AddAPICommands(root) @@ -510,6 +549,9 @@ func TestAPISDKCmd_Good_ValidatesLanguage(t *testing.T) { if sdkCmd.Flag("graphql-path") == nil { t.Fatal("expected --graphql-path flag on sdk command") } + if sdkCmd.Flag("sse-path") == nil { + t.Fatal("expected --sse-path flag on sdk command") + } if sdkCmd.Flag("terms-of-service") == nil { t.Fatal("expected --terms-of-service flag on sdk command") } @@ -548,6 +590,7 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) { "Custom SDK description", "9.9.9", "/gql", + "/events", "https://example.com/terms", "SDK Support", "https://example.com/support", @@ -596,6 +639,9 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) { if _, ok := paths["/gql"]; !ok { t.Fatal("expected GraphQL path to be included in generated spec") } + if _, ok := paths["/events"]; !ok { + t.Fatal("expected SSE 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"])