diff --git a/cmd/api/cmd_sdk.go b/cmd/api/cmd_sdk.go index 24011cf..806f026 100644 --- a/cmd/api/cmd_sdk.go +++ b/cmd/api/cmd_sdk.go @@ -32,6 +32,7 @@ func addSDKCommand(parent *cli.Command) { title string description string version string + graphqlPath string termsURL string contactName string contactURL string @@ -51,7 +52,7 @@ func addSDKCommand(parent *cli.Command) { // If no spec file provided, generate one to a temp file. if specFile == "" { - builder := sdkSpecBuilder(title, description, version, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers) + builder := sdkSpecBuilder(title, description, version, graphqlPath, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers) groups := sdkSpecGroupsIter() tmpFile, err := os.CreateTemp("", "openapi-*.json") @@ -100,6 +101,7 @@ func addSDKCommand(parent *cli.Command) { cli.StringFlag(cmd, &title, "title", "t", defaultSDKTitle, "API title in generated spec") 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, &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") @@ -113,11 +115,12 @@ func addSDKCommand(parent *cli.Command) { parent.AddCommand(cmd) } -func sdkSpecBuilder(title, description, version, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers string) *goapi.SpecBuilder { +func sdkSpecBuilder(title, description, version, graphqlPath, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers string) *goapi.SpecBuilder { return &goapi.SpecBuilder{ Title: title, Description: description, Version: version, + GraphQLPath: graphqlPath, TermsOfService: termsURL, ContactName: contactName, ContactURL: contactURL, diff --git a/cmd/api/cmd_spec.go b/cmd/api/cmd_spec.go index f22ba07..bd777e1 100644 --- a/cmd/api/cmd_spec.go +++ b/cmd/api/cmd_spec.go @@ -18,6 +18,7 @@ func addSpecCommand(parent *cli.Command) { title string description string version string + graphqlPath string termsURL string contactName string contactURL string @@ -35,6 +36,7 @@ func addSpecCommand(parent *cli.Command) { Title: title, Description: description, Version: version, + GraphQLPath: graphqlPath, TermsOfService: termsURL, ContactName: contactName, ContactURL: contactURL, @@ -65,6 +67,7 @@ func addSpecCommand(parent *cli.Command) { cli.StringFlag(cmd, &title, "title", "t", "Lethean Core API", "API title in spec") 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, &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 6429d7f..4f54b47 100644 --- a/cmd/api/cmd_test.go +++ b/cmd/api/cmd_test.go @@ -82,6 +82,9 @@ func TestAPISpecCmd_Good_JSON(t *testing.T) { if specCmd.Flag("version") == nil { t.Fatal("expected --version flag on spec command") } + if specCmd.Flag("graphql-path") == nil { + t.Fatal("expected --graphql-path flag on spec command") + } if specCmd.Flag("terms-of-service") == nil { t.Fatal("expected --terms-of-service flag on spec command") } @@ -402,6 +405,42 @@ func TestAPISpecCmd_Good_LicenseFlagsPopulateSpecInfo(t *testing.T) { } } +func TestAPISpecCmd_Good_GraphQLPathPopulatesSpec(t *testing.T) { + root := &cli.Command{Use: "root"} + AddAPICommands(root) + + outputFile := t.TempDir() + "/spec.json" + root.SetArgs([]string{ + "api", "spec", + "--graphql-path", "/gql", + "--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["/gql"]; !ok { + t.Fatal("expected GraphQL path to be included in generated spec") + } +} + func TestAPISDKCmd_Bad_EmptyLanguages(t *testing.T) { root := &cli.Command{Use: "root"} AddAPICommands(root) @@ -468,6 +507,9 @@ func TestAPISDKCmd_Good_ValidatesLanguage(t *testing.T) { if sdkCmd.Flag("version") == nil { t.Fatal("expected --version flag on sdk command") } + if sdkCmd.Flag("graphql-path") == nil { + t.Fatal("expected --graphql-path flag on sdk command") + } if sdkCmd.Flag("terms-of-service") == nil { t.Fatal("expected --terms-of-service flag on sdk command") } @@ -505,6 +547,7 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) { "Custom SDK API", "Custom SDK description", "9.9.9", + "/gql", "https://example.com/terms", "SDK Support", "https://example.com/support", @@ -546,6 +589,14 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) { t.Fatalf("expected custom version, got %v", info["version"]) } + paths, ok := spec["paths"].(map[string]any) + if !ok { + t.Fatalf("expected paths object in generated spec, got %T", spec["paths"]) + } + if _, ok := paths["/gql"]; !ok { + t.Fatal("expected GraphQL 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"]) }