diff --git a/cmd/api/cmd_sdk.go b/cmd/api/cmd_sdk.go index 4da9401..0b296fa 100644 --- a/cmd/api/cmd_sdk.go +++ b/cmd/api/cmd_sdk.go @@ -31,6 +31,8 @@ func addSDKCommand(parent *cli.Command) { title string description string version string + licenseName string + licenseURL string servers string ) @@ -42,7 +44,7 @@ func addSDKCommand(parent *cli.Command) { // If no spec file provided, generate one to a temp file. if specFile == "" { - builder := sdkSpecBuilder(title, description, version, servers) + builder := sdkSpecBuilder(title, description, version, licenseName, licenseURL, servers) groups := sdkSpecGroups() tmpFile, err := os.CreateTemp("", "openapi-*.json") @@ -91,17 +93,21 @@ 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, &licenseName, "license-name", "", "", "OpenAPI licence name in generated spec") + cli.StringFlag(cmd, &licenseURL, "license-url", "", "", "OpenAPI licence URL in generated spec") cli.StringFlag(cmd, &servers, "server", "S", "", "Comma-separated OpenAPI server URL(s)") parent.AddCommand(cmd) } -func sdkSpecBuilder(title, description, version, servers string) *goapi.SpecBuilder { +func sdkSpecBuilder(title, description, version, licenseName, licenseURL, servers string) *goapi.SpecBuilder { return &goapi.SpecBuilder{ Title: title, Description: description, Version: version, Servers: parseServers(servers), + LicenseName: licenseName, + LicenseURL: licenseURL, } } diff --git a/cmd/api/cmd_spec.go b/cmd/api/cmd_spec.go index 782340f..b3b77b6 100644 --- a/cmd/api/cmd_spec.go +++ b/cmd/api/cmd_spec.go @@ -18,6 +18,8 @@ func addSpecCommand(parent *cli.Command) { title string description string version string + licenseName string + licenseURL string servers string ) @@ -28,6 +30,8 @@ func addSpecCommand(parent *cli.Command) { Description: description, Version: version, Servers: parseServers(servers), + LicenseName: licenseName, + LicenseURL: licenseURL, } bridge := goapi.NewToolBridge("/tools") @@ -49,6 +53,8 @@ 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, &licenseName, "license-name", "", "", "OpenAPI licence name in spec") + cli.StringFlag(cmd, &licenseURL, "license-url", "", "", "OpenAPI licence URL in spec") cli.StringFlag(cmd, &servers, "server", "S", "", "Comma-separated OpenAPI server URL(s)") parent.AddCommand(cmd) diff --git a/cmd/api/cmd_test.go b/cmd/api/cmd_test.go index 10b0384..2a32375 100644 --- a/cmd/api/cmd_test.go +++ b/cmd/api/cmd_test.go @@ -82,6 +82,12 @@ func TestAPISpecCmd_Good_JSON(t *testing.T) { if specCmd.Flag("version") == nil { t.Fatal("expected --version flag on spec command") } + if specCmd.Flag("license-name") == nil { + t.Fatal("expected --license-name flag on spec command") + } + if specCmd.Flag("license-url") == nil { + t.Fatal("expected --license-url flag on spec command") + } if specCmd.Flag("server") == nil { t.Fatal("expected --server flag on spec command") } @@ -188,6 +194,50 @@ func TestAPISpecCmd_Good_RegisteredSpecGroups(t *testing.T) { } } +func TestAPISpecCmd_Good_LicenseFlagsPopulateSpecInfo(t *testing.T) { + root := &cli.Command{Use: "root"} + AddAPICommands(root) + + outputFile := t.TempDir() + "/spec.json" + root.SetArgs([]string{ + "api", "spec", + "--license-name", "EUPL-1.2", + "--license-url", "https://eupl.eu/1.2/en/", + "--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) + } + + info, ok := spec["info"].(map[string]any) + if !ok { + t.Fatal("expected info object in generated spec") + } + + license, ok := info["license"].(map[string]any) + if !ok { + t.Fatal("expected license metadata in generated spec") + } + if license["name"] != "EUPL-1.2" { + t.Fatalf("expected licence name EUPL-1.2, got %v", license["name"]) + } + if license["url"] != "https://eupl.eu/1.2/en/" { + t.Fatalf("expected licence url to be preserved, got %v", license["url"]) + } +} + func TestAPISDKCmd_Bad_EmptyLanguages(t *testing.T) { root := &cli.Command{Use: "root"} AddAPICommands(root) @@ -254,6 +304,12 @@ func TestAPISDKCmd_Good_ValidatesLanguage(t *testing.T) { if sdkCmd.Flag("version") == nil { t.Fatal("expected --version flag on sdk command") } + if sdkCmd.Flag("license-name") == nil { + t.Fatal("expected --license-name flag on sdk command") + } + if sdkCmd.Flag("license-url") == nil { + t.Fatal("expected --license-url flag on sdk command") + } if sdkCmd.Flag("server") == nil { t.Fatal("expected --server flag on sdk command") } @@ -269,7 +325,14 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) { api.RegisterSpecGroups(specCmdStubGroup{}) - builder := sdkSpecBuilder("Custom SDK API", "Custom SDK description", "9.9.9", "https://api.example.com, /, https://api.example.com") + builder := sdkSpecBuilder( + "Custom SDK API", + "Custom SDK description", + "9.9.9", + "EUPL-1.2", + "https://eupl.eu/1.2/en/", + "https://api.example.com, /, https://api.example.com", + ) groups := sdkSpecGroups() outputFile := t.TempDir() + "/spec.json" @@ -301,6 +364,17 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) { t.Fatalf("expected custom version, got %v", info["version"]) } + license, ok := info["license"].(map[string]any) + if !ok { + t.Fatal("expected licence metadata in generated spec") + } + if license["name"] != "EUPL-1.2" { + t.Fatalf("expected licence name EUPL-1.2, got %v", license["name"]) + } + if license["url"] != "https://eupl.eu/1.2/en/" { + t.Fatalf("expected licence url to be preserved, got %v", license["url"]) + } + servers, ok := spec["servers"].([]any) if !ok { t.Fatalf("expected servers array in generated spec, got %T", spec["servers"])