Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/httpapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"slices"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 ""
}
Expand Down
27 changes: 0 additions & 27 deletions lib/httpapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -79,9 +55,6 @@ func TestOpenAPISchema(t *testing.T) {
t.Fatalf("failed to unmarshal disk schema: %s", err)
}

normalizeSchema(t, &currentSchema)
normalizeSchema(t, &diskSchema)

require.Equal(t, currentSchema, diskSchema)
}

Expand Down
16 changes: 8 additions & 8 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"schemas": {
"AgentStatus": {
"enum": [
"stable",
"running"
"running",
"stable"
],
"examples": [
"stable"
Expand All @@ -14,8 +14,8 @@
},
"ConversationRole": {
"enum": [
"user",
"agent"
"agent",
"user"
],
"examples": [
"user"
Expand Down Expand Up @@ -130,8 +130,8 @@
}
},
"required": [
"id",
"content",
"id",
"role",
"time"
],
Expand Down Expand Up @@ -191,8 +191,8 @@
},
"MessageType": {
"enum": [
"user",
"raw"
"raw",
"user"
],
"examples": [
"user"
Expand Down Expand Up @@ -224,8 +224,8 @@
},
"required": [
"id",
"role",
"message",
"role",
"time"
],
"type": "object"
Expand Down