141 lines
4.5 KiB
Go
141 lines
4.5 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"Rezina-Backend/docs"
|
|
)
|
|
|
|
// Swagger 2 uses allOf to specialize a shared response envelope. Resolve those
|
|
// overrides before inspecting data, so an empty base interface cannot hide an
|
|
// undocumented response and a typed override is not mistaken for an empty field.
|
|
func responseSchemaObject(t *testing.T, spec, schema map[string]any, seen map[string]bool) map[string]any {
|
|
t.Helper()
|
|
if reference, ok := schema["$ref"].(string); ok {
|
|
if seen[reference] {
|
|
return map[string]any{"type": "object", "description": "Recursive schema reference"}
|
|
}
|
|
seen[reference] = true
|
|
definitions := spec["definitions"].(map[string]any)
|
|
definition, ok := definitions[strings.TrimPrefix(reference, "#/definitions/")].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("missing response schema %s", reference)
|
|
}
|
|
return responseSchemaObject(t, spec, definition, seen)
|
|
}
|
|
result := map[string]any{}
|
|
merge := func(part map[string]any) {
|
|
for key, value := range part {
|
|
if key == "properties" {
|
|
properties, _ := result[key].(map[string]any)
|
|
if properties == nil {
|
|
properties = map[string]any{}
|
|
result[key] = properties
|
|
}
|
|
for name, property := range value.(map[string]any) {
|
|
properties[name] = property
|
|
}
|
|
} else {
|
|
result[key] = value
|
|
}
|
|
}
|
|
}
|
|
if parts, ok := schema["allOf"].([]any); ok {
|
|
for _, part := range parts {
|
|
merge(responseSchemaObject(t, spec, part.(map[string]any), cloneSchemaSeen(seen)))
|
|
}
|
|
}
|
|
merge(schema)
|
|
delete(result, "allOf")
|
|
return result
|
|
}
|
|
|
|
func cloneSchemaSeen(source map[string]bool) map[string]bool {
|
|
result := map[string]bool{}
|
|
for k, v := range source {
|
|
result[k] = v
|
|
}
|
|
return result
|
|
}
|
|
|
|
func checkResponseSchemaDetail(t *testing.T, spec, schema map[string]any, path string, seen map[string]bool) {
|
|
t.Helper()
|
|
schema = responseSchemaObject(t, spec, schema, seen)
|
|
kind, _ := schema["type"].(string)
|
|
description, _ := schema["description"].(string)
|
|
if kind == "" {
|
|
// Dynamic question components are intentionally polymorphic. Their allowed
|
|
// JSON types and kind-dependent interpretation must be explicit.
|
|
if schema["x-json-types"] != nil && description != "" {
|
|
return
|
|
}
|
|
t.Errorf("%s: response field has no type or documented JSON variants", path)
|
|
return
|
|
}
|
|
if kind == "array" {
|
|
items, ok := schema["items"].(map[string]any)
|
|
if !ok {
|
|
t.Errorf("%s: array has no item schema", path)
|
|
return
|
|
}
|
|
checkResponseSchemaDetail(t, spec, items, path+"[]", cloneSchemaSeen(seen))
|
|
}
|
|
if kind == "object" {
|
|
properties, _ := schema["properties"].(map[string]any)
|
|
additional, hasAdditional := schema["additionalProperties"]
|
|
if len(properties) == 0 && !hasAdditional && description == "" {
|
|
t.Errorf("%s: object has no fields or documented extension semantics", path)
|
|
}
|
|
if values, ok := additional.(map[string]any); ok {
|
|
if len(values) == 0 {
|
|
if description == "" {
|
|
t.Errorf("%s: arbitrary JSON dictionary needs a description", path)
|
|
}
|
|
} else {
|
|
checkResponseSchemaDetail(t, spec, values, path+"{}", cloneSchemaSeen(seen))
|
|
}
|
|
}
|
|
for name, property := range properties {
|
|
checkResponseSchemaDetail(t, spec, property.(map[string]any), path+"."+name, cloneSchemaSeen(seen))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEveryAPIResponseHasDetailedSchema(t *testing.T) {
|
|
var spec map[string]any
|
|
if err := json.Unmarshal([]byte(docs.SwaggerInfo.ReadDoc()), &spec); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for path, raw := range spec["paths"].(map[string]any) {
|
|
for method, value := range raw.(map[string]any) {
|
|
operation := value.(map[string]any)
|
|
for code, value := range operation["responses"].(map[string]any) {
|
|
status, err := strconv.Atoi(code)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Redirects and protocol upgrades are described by status/headers,
|
|
// rather than pretending that they return a JSON document.
|
|
if status < 200 || (status >= 300 && status < 400) || status == 204 {
|
|
continue
|
|
}
|
|
t.Run(fmt.Sprintf("%s %s %s", method, path, code), func(t *testing.T) {
|
|
response := value.(map[string]any)
|
|
schema, ok := response["schema"].(map[string]any)
|
|
if !ok {
|
|
t.Fatal("response is missing its schema")
|
|
}
|
|
if status < 300 && (schema["$ref"] == "#/definitions/domain.Response" || schema["$ref"] == "#/definitions/response.APIResponse") {
|
|
t.Fatal("success uses an unspecialized generic response envelope")
|
|
}
|
|
checkResponseSchemaDetail(t, spec, schema, "response", map[string]bool{})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|