Skip to content

Commit 975390c

Browse files
committed
normalize request enums
1 parent 7764e41 commit 975390c

File tree

2 files changed

+154
-6
lines changed

2 files changed

+154
-6
lines changed

scripts/normalize/enums.go

Lines changed: 151 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,41 @@ func transformEnumProperty(enumInfo EnumNormalizationInfo) *ConflictDetail {
142142
}
143143

144144
func generateEnumMemberName(value, fieldName, entityName string) string {
145-
cleanEntityName := convertToEnumMemberName(strings.TrimSuffix(entityName, "Entity"))
145+
if strings.TrimSpace(value) == "" {
146+
value = "Empty"
147+
}
148+
149+
var prefix string
150+
151+
switch {
152+
case entityName == "RequestParam":
153+
prefix = "Request"
154+
case entityName == "RequestBody":
155+
if strings.HasPrefix(fieldName, "Form") {
156+
prefix = "" // Form prefix is already in fieldName
157+
} else {
158+
prefix = "Request"
159+
}
160+
case entityName == "Response":
161+
prefix = "Response"
162+
case strings.HasSuffix(entityName, "Entity"):
163+
prefix = convertToEnumMemberName(strings.TrimSuffix(entityName, "Entity"))
164+
default:
165+
prefix = convertToEnumMemberName(entityName)
166+
}
167+
146168
cleanFieldName := convertToEnumMemberName(fieldName)
147169
cleanValue := convertToEnumMemberName(value)
148170

149-
memberName := cleanEntityName + cleanFieldName + cleanValue
171+
memberName := prefix + cleanFieldName + cleanValue
150172

151173
// Ensure it starts with a letter
152174
if len(memberName) > 0 && unicode.IsDigit(rune(memberName[0])) {
153175
memberName = "Value" + memberName
154176
}
155177

156-
// Handle empty result after cleaning
157178
if memberName == "" {
158-
memberName = cleanEntityName + cleanFieldName + "Unknown"
179+
memberName = prefix + cleanFieldName + "Unknown"
159180
}
160181

161182
return memberName
@@ -216,15 +237,139 @@ func convertToEnumMemberName(value string) string {
216237

217238
memberName := result.String()
218239

219-
// Ensure it starts with a letter
220240
if len(memberName) > 0 && unicode.IsDigit(rune(memberName[0])) {
221241
memberName = "Value" + memberName
222242
}
223243

224-
// Handle empty result after cleaning
225244
if memberName == "" {
226245
memberName = "Empty"
227246
}
228247

229248
return memberName
230249
}
250+
251+
func normalizePathEnums(paths map[string]interface{}) []ConflictDetail {
252+
conflicts := make([]ConflictDetail, 0)
253+
254+
fmt.Printf("\n=== Normalizing Path/Operation Enum Properties ===\n")
255+
256+
allPathEnums := findAllPathEnumProperties(paths)
257+
258+
if len(allPathEnums) == 0 {
259+
fmt.Printf("No path enum properties found to normalize\n")
260+
return conflicts
261+
}
262+
263+
fmt.Printf("Found %d path enum properties to normalize\n", len(allPathEnums))
264+
265+
for _, enumInfo := range allPathEnums {
266+
conflict := transformEnumProperty(enumInfo)
267+
if conflict != nil {
268+
conflicts = append(conflicts, *conflict)
269+
}
270+
}
271+
272+
fmt.Printf("Successfully normalized %d path enum properties\n", len(conflicts))
273+
return conflicts
274+
}
275+
276+
func findAllPathEnumProperties(paths map[string]interface{}) []EnumNormalizationInfo {
277+
var allEnums []EnumNormalizationInfo
278+
279+
for pathName, pathItem := range paths {
280+
pathMap, ok := pathItem.(map[string]interface{})
281+
if !ok {
282+
continue
283+
}
284+
285+
methods := []string{"get", "post", "put", "patch", "delete"}
286+
for _, method := range methods {
287+
if operation, exists := pathMap[method]; exists {
288+
opMap, ok := operation.(map[string]interface{})
289+
if !ok {
290+
continue
291+
}
292+
293+
if parameters, hasParams := opMap["parameters"]; hasParams {
294+
paramsList, ok := parameters.([]interface{})
295+
if ok {
296+
for _, param := range paramsList {
297+
paramMap, ok := param.(map[string]interface{})
298+
if ok {
299+
paramName, _ := paramMap["name"].(string)
300+
schemaName := fmt.Sprintf("RequestParam_%s_%s_%s", method, pathName, paramName)
301+
302+
if schema, hasSchema := paramMap["schema"].(map[string]interface{}); hasSchema {
303+
enumEnums := findEnumsInSchemaRecursive(schemaName, schema, "", schema)
304+
for j := range enumEnums {
305+
enumEnums[j].PropertyName = convertToEnumMemberName(paramName)
306+
enumEnums[j].SchemaName = "RequestParam"
307+
}
308+
allEnums = append(allEnums, enumEnums...)
309+
}
310+
}
311+
}
312+
}
313+
}
314+
315+
if requestBody, hasReqBody := opMap["requestBody"]; hasReqBody {
316+
reqBodyMap, ok := requestBody.(map[string]interface{})
317+
if ok {
318+
if content, hasContent := reqBodyMap["content"].(map[string]interface{}); hasContent {
319+
for contentType, contentSchema := range content {
320+
if contentMap, ok := contentSchema.(map[string]interface{}); ok {
321+
if schema, hasSchema := contentMap["schema"].(map[string]interface{}); hasSchema {
322+
schemaName := fmt.Sprintf("RequestBody_%s_%s", method, sanitizePathForSchemaName(pathName))
323+
reqEnums := findEnumsInSchemaRecursive(schemaName, schema, "", schema)
324+
325+
for j := range reqEnums {
326+
if contentType == "multipart/form-data" {
327+
reqEnums[j].PropertyName = "Form" + convertToEnumMemberName(reqEnums[j].PropertyName)
328+
} else {
329+
reqEnums[j].PropertyName = "Request" + convertToEnumMemberName(reqEnums[j].PropertyName)
330+
}
331+
reqEnums[j].SchemaName = "RequestBody"
332+
}
333+
allEnums = append(allEnums, reqEnums...)
334+
335+
fmt.Printf("📋 Found request body enum in %s %s (%s)\n", method, pathName, contentType)
336+
}
337+
}
338+
}
339+
}
340+
}
341+
}
342+
343+
// Checks for responses with inline enums enums, uncommon but included as a safeguard
344+
if responses, hasResponses := opMap["responses"]; hasResponses {
345+
respMap, ok := responses.(map[string]interface{})
346+
if ok {
347+
for statusCode, response := range respMap {
348+
if respBody, ok := response.(map[string]interface{}); ok {
349+
if content, hasContent := respBody["content"].(map[string]interface{}); hasContent {
350+
if jsonContent, hasJson := content["application/json"].(map[string]interface{}); hasJson {
351+
if schema, hasSchema := jsonContent["schema"].(map[string]interface{}); hasSchema {
352+
schemaName := fmt.Sprintf("Response_%s_%s_%s", method, pathName, statusCode)
353+
respEnums := findEnumsInSchemaRecursive(schemaName, schema, "", schema)
354+
allEnums = append(allEnums, respEnums...)
355+
}
356+
}
357+
}
358+
}
359+
}
360+
}
361+
}
362+
}
363+
}
364+
}
365+
366+
return allEnums
367+
}
368+
369+
func sanitizePathForSchemaName(path string) string {
370+
cleaned := strings.ReplaceAll(path, "/", "_")
371+
cleaned = strings.ReplaceAll(cleaned, "{", "")
372+
cleaned = strings.ReplaceAll(cleaned, "}", "")
373+
cleaned = strings.Trim(cleaned, "_")
374+
return cleaned
375+
}

scripts/normalize/normalize.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func normalizeSpec(spec map[string]interface{}) NormalizationReport {
6060
if pathsOk {
6161
parameterFixes := normalizePathParameters(paths)
6262
report.ConflictDetails = append(report.ConflictDetails, parameterFixes...)
63+
64+
pathEnumFixes := normalizePathEnums(paths)
65+
report.ConflictDetails = append(report.ConflictDetails, pathEnumFixes...)
6366
}
6467

6568
report.TotalFixes = len(report.ConflictDetails)

0 commit comments

Comments
 (0)