From 1159ad3f6d333bc98595b3c6fcddb6cec003e373 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Sun, 28 Sep 2025 23:24:00 +0530 Subject: [PATCH 1/3] feat: normalized openapi.json --- openapi.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/openapi.json b/openapi.json index 4a5fb83..a92df5b 100644 --- a/openapi.json +++ b/openapi.json @@ -3,8 +3,8 @@ "schemas": { "AgentStatus": { "enum": [ - "stable", - "running" + "running", + "stable" ], "examples": [ "stable" @@ -14,8 +14,8 @@ }, "ConversationRole": { "enum": [ - "user", - "agent" + "agent", + "user" ], "examples": [ "user" @@ -130,8 +130,8 @@ } }, "required": [ - "id", "content", + "id", "role", "time" ], @@ -191,8 +191,8 @@ }, "MessageType": { "enum": [ - "user", - "raw" + "raw", + "user" ], "examples": [ "user" @@ -224,8 +224,8 @@ }, "required": [ "id", - "role", "message", + "role", "time" ], "type": "object" From b98b2c6c08c57eb270e7f53754a755f10f61ab84 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Sun, 28 Sep 2025 23:27:16 +0530 Subject: [PATCH 2/3] feat: normalized openapi.json --- lib/httpapi/server.go | 28 +++++++++++++++++++++++++++- lib/httpapi/server_test.go | 28 ++-------------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/httpapi/server.go b/lib/httpapi/server.go index 90fd48f..08d92c4 100644 --- a/lib/httpapi/server.go +++ b/lib/httpapi/server.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "slices" + "sort" "strings" "sync" "time" @@ -41,6 +42,27 @@ type Server struct { chatBasePath string } +func (s *Server) NormalizeSchema(schema any) any { + switch val := (schema).(type) { + case *any: + s.NormalizeSchema(*val) + case []any: + for i := range val { + s.NormalizeSchema(&val[i]) + } + sort.SliceStable(val, func(i, j int) bool { + return fmt.Sprintf("%v", val[i]) < fmt.Sprintf("%v", val[j]) + }) + case map[string]any: + for k := range val { + valUnderKey := val[k] + s.NormalizeSchema(&valUnderKey) + val[k] = valUnderKey + } + } + return schema +} + func (s *Server) GetOpenAPI() string { jsonBytes, err := s.api.OpenAPI().MarshalJSON() if err != nil { @@ -51,7 +73,11 @@ func (s *Server) GetOpenAPI() string { if err := json.Unmarshal(jsonBytes, &jsonObj); err != nil { return "" } - prettyJSON, err := json.MarshalIndent(jsonObj, "", " ") + + // Normalize + normalized := s.NormalizeSchema(jsonObj) + + prettyJSON, err := json.MarshalIndent(normalized, "", " ") if err != nil { return "" } diff --git a/lib/httpapi/server_test.go b/lib/httpapi/server_test.go index 3778fc7..9b8de19 100644 --- a/lib/httpapi/server_test.go +++ b/lib/httpapi/server_test.go @@ -3,13 +3,11 @@ package httpapi_test import ( "context" "encoding/json" - "fmt" "io" "log/slog" "net/http" "net/http/httptest" "os" - "sort" "testing" "github.com/coder/agentapi/lib/httpapi" @@ -19,28 +17,6 @@ import ( "github.com/stretchr/testify/require" ) -func normalizeSchema(t *testing.T, schema any) any { - t.Helper() - switch val := (schema).(type) { - case *any: - normalizeSchema(t, *val) - case []any: - for i := range val { - normalizeSchema(t, &val[i]) - } - sort.SliceStable(val, func(i, j int) bool { - return fmt.Sprintf("%v", val[i]) < fmt.Sprintf("%v", val[j]) - }) - case map[string]any: - for k := range val { - valUnderKey := val[k] - normalizeSchema(t, &valUnderKey) - val[k] = valUnderKey - } - } - return schema -} - // Ensure the OpenAPI schema on disk is up to date. // To update the schema, run `go run main.go server --print-openapi dummy > openapi.json`. func TestOpenAPISchema(t *testing.T) { @@ -79,8 +55,8 @@ func TestOpenAPISchema(t *testing.T) { t.Fatalf("failed to unmarshal disk schema: %s", err) } - normalizeSchema(t, ¤tSchema) - normalizeSchema(t, &diskSchema) + srv.NormalizeSchema(¤tSchema) + srv.NormalizeSchema(&diskSchema) require.Equal(t, currentSchema, diskSchema) } From 38d446bf3f7c64479e307c292510917fc243e8db Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Sun, 28 Sep 2025 23:33:43 +0530 Subject: [PATCH 3/3] chore: remove normalization from tests --- lib/httpapi/server_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/httpapi/server_test.go b/lib/httpapi/server_test.go index 9b8de19..c209638 100644 --- a/lib/httpapi/server_test.go +++ b/lib/httpapi/server_test.go @@ -55,9 +55,6 @@ func TestOpenAPISchema(t *testing.T) { t.Fatalf("failed to unmarshal disk schema: %s", err) } - srv.NormalizeSchema(¤tSchema) - srv.NormalizeSchema(&diskSchema) - require.Equal(t, currentSchema, diskSchema) }