Skip to content

Commit a9160eb

Browse files
authored
fix: inconsistent openapi gen (#102)
1 parent d0b5ad0 commit a9160eb

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
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: 0 additions & 27 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,9 +55,6 @@ 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)
84-
8558
require.Equal(t, currentSchema, diskSchema)
8659
}
8760

openapi.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"schemas": {
44
"AgentStatus": {
55
"enum": [
6-
"stable",
7-
"running"
6+
"running",
7+
"stable"
88
],
99
"examples": [
1010
"stable"
@@ -14,8 +14,8 @@
1414
},
1515
"ConversationRole": {
1616
"enum": [
17-
"user",
18-
"agent"
17+
"agent",
18+
"user"
1919
],
2020
"examples": [
2121
"user"
@@ -130,8 +130,8 @@
130130
}
131131
},
132132
"required": [
133-
"id",
134133
"content",
134+
"id",
135135
"role",
136136
"time"
137137
],
@@ -191,8 +191,8 @@
191191
},
192192
"MessageType": {
193193
"enum": [
194-
"user",
195-
"raw"
194+
"raw",
195+
"user"
196196
],
197197
"examples": [
198198
"user"
@@ -224,8 +224,8 @@
224224
},
225225
"required": [
226226
"id",
227-
"role",
228227
"message",
228+
"role",
229229
"time"
230230
],
231231
"type": "object"

0 commit comments

Comments
 (0)