|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +type RequestSchemaInfo struct { |
| 9 | + Path string |
| 10 | + Method string |
| 11 | + OperationId string |
| 12 | + ContentType string |
| 13 | + Schema map[string]interface{} |
| 14 | + SchemaName string |
| 15 | + IsInline bool |
| 16 | +} |
| 17 | + |
| 18 | +func findInlineRequestSchemas(paths map[string]interface{}) []RequestSchemaInfo { |
| 19 | + var inlineSchemas []RequestSchemaInfo |
| 20 | + |
| 21 | + for pathName, pathItem := range paths { |
| 22 | + pathMap, ok := pathItem.(map[string]interface{}) |
| 23 | + if !ok { |
| 24 | + continue |
| 25 | + } |
| 26 | + |
| 27 | + // Check POST, PUT, PATCH methods for request bodies |
| 28 | + methods := []string{"post", "put", "patch"} |
| 29 | + for _, method := range methods { |
| 30 | + if operation, exists := pathMap[method]; exists { |
| 31 | + opMap, ok := operation.(map[string]interface{}) |
| 32 | + if !ok { |
| 33 | + continue |
| 34 | + } |
| 35 | + |
| 36 | + operationId, _ := opMap["operationId"].(string) |
| 37 | + |
| 38 | + if requestBody, hasReqBody := opMap["requestBody"]; hasReqBody { |
| 39 | + reqBodyMap, ok := requestBody.(map[string]interface{}) |
| 40 | + if !ok { |
| 41 | + continue |
| 42 | + } |
| 43 | + |
| 44 | + if content, hasContent := reqBodyMap["content"].(map[string]interface{}); hasContent { |
| 45 | + for contentType, contentSchema := range content { |
| 46 | + if contentMap, ok := contentSchema.(map[string]interface{}); ok { |
| 47 | + if schema, hasSchema := contentMap["schema"].(map[string]interface{}); hasSchema { |
| 48 | + // Check if it's an inline schema (no $ref) |
| 49 | + if _, hasRef := schema["$ref"]; !hasRef { |
| 50 | + // Generate schema name |
| 51 | + schemaName := generateRequestSchemaName(operationId, contentType) |
| 52 | + |
| 53 | + inlineSchemas = append(inlineSchemas, RequestSchemaInfo{ |
| 54 | + Path: pathName, |
| 55 | + Method: method, |
| 56 | + OperationId: operationId, |
| 57 | + ContentType: contentType, |
| 58 | + Schema: schema, |
| 59 | + SchemaName: schemaName, |
| 60 | + IsInline: true, |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return inlineSchemas |
| 73 | +} |
| 74 | + |
| 75 | +func generateRequestSchemaName(operationId, contentType string) string { |
| 76 | + // Tests in laddertruck required operationIds to be added to routes |
| 77 | + // so we will rely on these here |
| 78 | + schemaName := operationId |
| 79 | + |
| 80 | + // Only add suffix for non-JSON content types |
| 81 | + if contentType != "application/json" { |
| 82 | + schemaName = schemaName + convertContentTypeToSuffix(contentType) |
| 83 | + } |
| 84 | + |
| 85 | + return schemaName |
| 86 | +} |
| 87 | + |
| 88 | +func convertContentTypeToSuffix(contentType string) string { |
| 89 | + switch contentType { |
| 90 | + case "multipart/form-data": |
| 91 | + return "_form" |
| 92 | + case "application/x-www-form-urlencoded": |
| 93 | + return "_form_encoded" |
| 94 | + case "text/plain": |
| 95 | + return "_text" |
| 96 | + case "application/xml": |
| 97 | + return "_xml" |
| 98 | + default: |
| 99 | + suffix := strings.ReplaceAll(contentType, "/", "_") |
| 100 | + suffix = strings.ReplaceAll(suffix, "-", "_") |
| 101 | + suffix = strings.ReplaceAll(suffix, ".", "_") |
| 102 | + suffix = strings.ReplaceAll(suffix, "+", "_") |
| 103 | + return "_" + suffix |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func ensureUniqueSchemaName(baseName string, schemas map[string]interface{}) string { |
| 108 | + if _, exists := schemas[baseName]; !exists { |
| 109 | + return baseName |
| 110 | + } |
| 111 | + |
| 112 | + counter := 1 |
| 113 | + for { |
| 114 | + candidate := fmt.Sprintf("%s_%d", baseName, counter) |
| 115 | + if _, exists := schemas[candidate]; !exists { |
| 116 | + return candidate |
| 117 | + } |
| 118 | + counter++ |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// normalizeRequestSchemasWithPaths extracts inline request body schemas and moves them to components/schemas |
| 123 | +func normalizeRequestSchemasWithPaths(paths map[string]interface{}, schemas map[string]interface{}) []ConflictDetail { |
| 124 | + conflicts := make([]ConflictDetail, 0) |
| 125 | + |
| 126 | + fmt.Printf("\n=== Normalizing Request Schemas ===\n") |
| 127 | + |
| 128 | + inlineSchemas := findInlineRequestSchemas(paths) |
| 129 | + |
| 130 | + if len(inlineSchemas) == 0 { |
| 131 | + fmt.Printf("No inline request schemas found to normalize\n") |
| 132 | + return conflicts |
| 133 | + } |
| 134 | + |
| 135 | + fmt.Printf("Found %d inline request schemas to normalize\n", len(inlineSchemas)) |
| 136 | + |
| 137 | + for _, schemaInfo := range inlineSchemas { |
| 138 | + finalSchemaName := ensureUniqueSchemaName(schemaInfo.SchemaName, schemas) |
| 139 | + |
| 140 | + schemaCopy := make(map[string]interface{}) |
| 141 | + for k, v := range schemaInfo.Schema { |
| 142 | + schemaCopy[k] = v |
| 143 | + } |
| 144 | + |
| 145 | + schemas[finalSchemaName] = schemaCopy |
| 146 | + |
| 147 | + // Replace the original inline schema with $ref |
| 148 | + ref := fmt.Sprintf("#/components/schemas/%s", finalSchemaName) |
| 149 | + |
| 150 | + // Clear the original schema and replace with $ref |
| 151 | + for key := range schemaInfo.Schema { |
| 152 | + delete(schemaInfo.Schema, key) |
| 153 | + } |
| 154 | + schemaInfo.Schema["$ref"] = ref |
| 155 | + |
| 156 | + conflicts = append(conflicts, ConflictDetail{ |
| 157 | + Schema: finalSchemaName, |
| 158 | + Property: fmt.Sprintf("%s.%s", schemaInfo.Method, schemaInfo.Path), |
| 159 | + ConflictType: "request-schema-extraction", |
| 160 | + Resolution: fmt.Sprintf("Extracted inline request schema to %s", finalSchemaName), |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + fmt.Printf("Successfully normalized %d request schemas\n", len(conflicts)) |
| 165 | + return conflicts |
| 166 | +} |
0 commit comments