|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal.openapi; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.LinkedHashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.function.Function; |
| 15 | + |
| 16 | +import org.objectweb.asm.Opcodes; |
| 17 | +import org.objectweb.asm.Type; |
| 18 | +import org.objectweb.asm.tree.ClassNode; |
| 19 | +import org.objectweb.asm.tree.FieldNode; |
| 20 | +import org.objectweb.asm.tree.MethodNode; |
| 21 | + |
| 22 | +import io.swagger.v3.oas.models.media.Schema; |
| 23 | + |
| 24 | +/** |
| 25 | + * Makes {@link Schema#getProperties()} order deterministic by reordering keys according to |
| 26 | + * class-file declaration order (fields, then getters), including the superclass chain. |
| 27 | + * |
| 28 | + * <p>This preserves a natural bean-like order |
| 29 | + */ |
| 30 | +public final class SchemaPropertyOrder { |
| 31 | + |
| 32 | + private SchemaPropertyOrder() {} |
| 33 | + |
| 34 | + public static void stabilize( |
| 35 | + ClassNode node, Schema<?> schema, Function<Type, ClassNode> classNodes) { |
| 36 | + Map<String, Schema> properties = schema.getProperties(); |
| 37 | + if (properties == null || properties.isEmpty() || node == null) { |
| 38 | + return; |
| 39 | + } |
| 40 | + List<String> declarationOrder = declarationOrder(node, classNodes); |
| 41 | + if (declarationOrder.isEmpty()) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var ordered = new LinkedHashMap<String, Schema>(); |
| 46 | + for (String name : declarationOrder) { |
| 47 | + Schema property = properties.get(name); |
| 48 | + if (property != null) { |
| 49 | + ordered.put(name, property); |
| 50 | + } |
| 51 | + } |
| 52 | + // Keep any leftover properties (e.g. synthetic names) in their original relative order. |
| 53 | + properties.forEach(ordered::putIfAbsent); |
| 54 | + schema.setProperties(ordered); |
| 55 | + } |
| 56 | + |
| 57 | + static List<String> declarationOrder(ClassNode node, Function<Type, ClassNode> classNodes) { |
| 58 | + var names = new LinkedHashSet<String>(); |
| 59 | + collect(node, classNodes, names); |
| 60 | + return new ArrayList<>(names); |
| 61 | + } |
| 62 | + |
| 63 | + private static void collect( |
| 64 | + ClassNode node, Function<Type, ClassNode> classNodes, Set<String> names) { |
| 65 | + if (node == null || isExcluded(node.name)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + if (node.superName != null && !isExcluded(node.superName)) { |
| 69 | + collect(classNodes.apply(Type.getObjectType(node.superName)), classNodes, names); |
| 70 | + } |
| 71 | + if (node.fields != null) { |
| 72 | + for (FieldNode field : node.fields) { |
| 73 | + if (isInstanceField(field)) { |
| 74 | + names.add(field.name); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + if (node.methods != null) { |
| 79 | + for (MethodNode method : node.methods) { |
| 80 | + if (isGetter(method)) { |
| 81 | + names.add(propertyName(method.name)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static boolean isExcluded(String internalName) { |
| 88 | + return internalName == null |
| 89 | + || internalName.equals("java/lang/Object") |
| 90 | + || internalName.equals("java/lang/Record") |
| 91 | + || internalName.equals("java/lang/Enum"); |
| 92 | + } |
| 93 | + |
| 94 | + private static boolean isInstanceField(FieldNode field) { |
| 95 | + return (field.access & Opcodes.ACC_STATIC) == 0 |
| 96 | + && (field.access & Opcodes.ACC_SYNTHETIC) == 0; |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean isGetter(MethodNode method) { |
| 100 | + if ((method.access & Opcodes.ACC_STATIC) != 0 |
| 101 | + || (method.access & Opcodes.ACC_PUBLIC) == 0 |
| 102 | + || (method.access & Opcodes.ACC_SYNTHETIC) != 0 |
| 103 | + || (method.access & Opcodes.ACC_BRIDGE) != 0) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + if (Type.getArgumentTypes(method.desc).length != 0) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + Type returnType = Type.getReturnType(method.desc); |
| 110 | + if (returnType.equals(Type.VOID_TYPE)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if (method.name.startsWith("get") && method.name.length() > 3) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + return method.name.startsWith("is") |
| 117 | + && method.name.length() > 2 |
| 118 | + && (returnType.equals(Type.BOOLEAN_TYPE) |
| 119 | + || returnType.getClassName().equals(Boolean.class.getName())); |
| 120 | + } |
| 121 | + |
| 122 | + private static String propertyName(String methodName) { |
| 123 | + if (methodName.startsWith("get")) { |
| 124 | + return decapitalize(methodName.substring(3)); |
| 125 | + } |
| 126 | + if (methodName.startsWith("is")) { |
| 127 | + return decapitalize(methodName.substring(2)); |
| 128 | + } |
| 129 | + return methodName; |
| 130 | + } |
| 131 | + |
| 132 | + /** Same rules as {@code java.beans.Introspector.decapitalize}. */ |
| 133 | + private static String decapitalize(String name) { |
| 134 | + if (name == null || name.isEmpty()) { |
| 135 | + return name; |
| 136 | + } |
| 137 | + if (name.length() > 1 |
| 138 | + && Character.isUpperCase(name.charAt(0)) |
| 139 | + && Character.isUpperCase(name.charAt(1))) { |
| 140 | + return name; |
| 141 | + } |
| 142 | + char[] chars = name.toCharArray(); |
| 143 | + chars[0] = Character.toLowerCase(chars[0]); |
| 144 | + return new String(chars); |
| 145 | + } |
| 146 | +} |
0 commit comments