feat(cmd/api): add GraphQL path to spec generation

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 00:21:20 +00:00
parent 68bf8dcaf8
commit 2c87fa02cb
3 changed files with 59 additions and 2 deletions

View file

@ -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,

View file

@ -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")

View file

@ -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"])
}