Skip to content

Commit b98b2c6

Browse files
committed
feat: normalized openapi.json
1 parent 1159ad3 commit b98b2c6

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

lib/httpapi/server.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"slices"
11+
"sort"
1112
"strings"
1213
"sync"
1314
"time"
@@ -41,6 +42,27 @@ type Server struct {
4142
chatBasePath string
4243
}
4344

45+
func (s *Server) NormalizeSchema(schema any) any {
46+
switch val := (schema).(type) {
47+
case *any:
48+
s.NormalizeSchema(*val)
49+
case []any:
50+
for i := range val {
51+
s.NormalizeSchema(&val[i])
52+
}
53+
sort.SliceStable(val, func(i, j int) bool {
54+
return fmt.Sprintf("%v", val[i]) < fmt.Sprintf("%v", val[j])
55+
})
56+
case map[string]any:
57+
for k := range val {
58+
valUnderKey := val[k]
59+
s.NormalizeSchema(&valUnderKey)
60+
val[k] = valUnderKey
61+
}
62+
}
63+
return schema
64+
}
65+
4466
func (s *Server) GetOpenAPI() string {
4567
jsonBytes, err := s.api.OpenAPI().MarshalJSON()
4668
if err != nil {
@@ -51,7 +73,11 @@ func (s *Server) GetOpenAPI() string {
5173
if err := json.Unmarshal(jsonBytes, &jsonObj); err != nil {
5274
return ""
5375
}
54-
prettyJSON, err := json.MarshalIndent(jsonObj, "", " ")
76+
77+
// Normalize
78+
normalized := s.NormalizeSchema(jsonObj)
79+
80+
prettyJSON, err := json.MarshalIndent(normalized, "", " ")
5581
if err != nil {
5682
return ""
5783
}

lib/httpapi/server_test.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package httpapi_test
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"io"
87
"log/slog"
98
"net/http"
109
"net/http/httptest"
1110
"os"
12-
"sort"
1311
"testing"
1412

1513
"github.com/coder/agentapi/lib/httpapi"
@@ -19,28 +17,6 @@ import (
1917
"github.com/stretchr/testify/require"
2018
)
2119

22-
func normalizeSchema(t *testing.T, schema any) any {
23-
t.Helper()
24-
switch val := (schema).(type) {
25-
case *any:
26-
normalizeSchema(t, *val)
27-
case []any:
28-
for i := range val {
29-
normalizeSchema(t, &val[i])
30-
}
31-
sort.SliceStable(val, func(i, j int) bool {
32-
return fmt.Sprintf("%v", val[i]) < fmt.Sprintf("%v", val[j])
33-
})
34-
case map[string]any:
35-
for k := range val {
36-
valUnderKey := val[k]
37-
normalizeSchema(t, &valUnderKey)
38-
val[k] = valUnderKey
39-
}
40-
}
41-
return schema
42-
}
43-
4420
// Ensure the OpenAPI schema on disk is up to date.
4521
// To update the schema, run `go run main.go server --print-openapi dummy > openapi.json`.
4622
func TestOpenAPISchema(t *testing.T) {
@@ -79,8 +55,8 @@ func TestOpenAPISchema(t *testing.T) {
7955
t.Fatalf("failed to unmarshal disk schema: %s", err)
8056
}
8157

82-
normalizeSchema(t, &currentSchema)
83-
normalizeSchema(t, &diskSchema)
58+
srv.NormalizeSchema(&currentSchema)
59+
srv.NormalizeSchema(&diskSchema)
8460

8561
require.Equal(t, currentSchema, diskSchema)
8662
}

0 commit comments

Comments
 (0)