|
| 1 | +package org.framefork.typedIds.swagger; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.type.SimpleType; |
| 4 | +import io.swagger.v3.core.converter.AnnotatedType; |
| 5 | +import io.swagger.v3.core.converter.ModelConverterContext; |
| 6 | +import io.swagger.v3.core.converter.ModelConverterContextImpl; |
| 7 | +import io.swagger.v3.core.converter.ModelConverters; |
| 8 | +import io.swagger.v3.oas.models.media.IntegerSchema; |
| 9 | +import io.swagger.v3.oas.models.media.Schema; |
| 10 | +import io.swagger.v3.oas.models.media.StringSchema; |
| 11 | +import org.framefork.typedIds.TypedId; |
| 12 | +import org.framefork.typedIds.bigint.ObjectBigIntId; |
| 13 | +import org.framefork.typedIds.uuid.ObjectUuid; |
| 14 | +import org.jetbrains.annotations.ApiStatus; |
| 15 | +import org.jspecify.annotations.Nullable; |
| 16 | + |
| 17 | +import java.lang.reflect.ParameterizedType; |
| 18 | +import java.lang.reflect.Type; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | +import java.util.function.Predicate; |
| 22 | + |
| 23 | +@SuppressWarnings("rawtypes") |
| 24 | +@ApiStatus.Internal |
| 25 | +public final class TypedIdsSchemaUtils |
| 26 | +{ |
| 27 | + |
| 28 | + public static final int UUID_LENGTH = 36; |
| 29 | + |
| 30 | + private final static ConcurrentHashMap<Class<?>, Schema> DEFAULT_SCHEMAS = new ConcurrentHashMap<>(); |
| 31 | + |
| 32 | + private TypedIdsSchemaUtils() |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + public static Schema createSchema(final Class<?> rawClass) |
| 37 | + { |
| 38 | + if (ObjectUuid.class.isAssignableFrom(rawClass)) { |
| 39 | + return createUuidSchema(rawClass); |
| 40 | + |
| 41 | + } else if (ObjectBigIntId.class.isAssignableFrom(rawClass)) { |
| 42 | + return createBigIntSchema(rawClass); |
| 43 | + } |
| 44 | + |
| 45 | + throw new IllegalArgumentException("Given class is not a subtype of %s, %s was given".formatted(TypedId.class, rawClass)); |
| 46 | + } |
| 47 | + |
| 48 | + public static Schema createUuidSchema(final Class<?> rawClass) |
| 49 | + { |
| 50 | + // Map UUID-based typed IDs to primitive string:uuid in OpenAPI |
| 51 | + var result = new StringSchema() |
| 52 | + .format("uuid") |
| 53 | + .minLength(UUID_LENGTH) |
| 54 | + .maxLength(UUID_LENGTH); |
| 55 | + |
| 56 | + return applyDefaultSchema(result, getDefaultSchema(rawClass)); |
| 57 | + } |
| 58 | + |
| 59 | + public static Schema createBigIntSchema(final Class<?> rawClass) |
| 60 | + { |
| 61 | + // Map bigint-based typed IDs to primitive integer:int64 in OpenAPI |
| 62 | + var result = new IntegerSchema() |
| 63 | + .format("int64"); |
| 64 | + |
| 65 | + return applyDefaultSchema(result, getDefaultSchema(rawClass)); |
| 66 | + } |
| 67 | + |
| 68 | + private static Schema applyDefaultSchema(final Schema result, final Schema defaultSchema) |
| 69 | + { |
| 70 | + Optional.ofNullable(defaultSchema.getName()) |
| 71 | + .filter(Predicate.not(String::isBlank)) |
| 72 | + .ifPresent(result::setName); |
| 73 | + Optional.ofNullable(defaultSchema.getDescription()) |
| 74 | + .filter(Predicate.not(String::isBlank)) |
| 75 | + .ifPresent(result::setDescription); |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + public static Schema getDefaultSchema(final Class<?> rawClass) |
| 80 | + { |
| 81 | + return DEFAULT_SCHEMAS.computeIfAbsent(rawClass, TypedIdsSchemaUtils::computeDefaultSchema); |
| 82 | + } |
| 83 | + |
| 84 | + private static Schema computeDefaultSchema(final Class<?> rawClass) |
| 85 | + { |
| 86 | + var converterContext = getModelConverterContext(); |
| 87 | + var schema = converterContext.resolve( |
| 88 | + new AnnotatedType() |
| 89 | + .type(rawClass) |
| 90 | + .resolveAsRef(false) |
| 91 | + ); |
| 92 | + |
| 93 | + var schemaAnnotation = rawClass.getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class); |
| 94 | + var declaredName = Optional.ofNullable(schemaAnnotation).map(io.swagger.v3.oas.annotations.media.Schema::name).filter(Predicate.not(String::isBlank)).isPresent(); |
| 95 | + if (!declaredName) { |
| 96 | + schema.setName(getAutomaticSchemaNameForClass(rawClass)); |
| 97 | + |
| 98 | + } else { |
| 99 | + if (schema.getName() == null || schema.getName().isBlank()) { |
| 100 | + schema.setName(getAutomaticSchemaNameForClass(rawClass)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (schema.getDescription() == null || schema.getDescription().isBlank()) { |
| 105 | + schema.setDescription("ID of type " + schema.getName()); |
| 106 | + } |
| 107 | + |
| 108 | + return schema; |
| 109 | + } |
| 110 | + |
| 111 | + @Nullable |
| 112 | + public static Class<?> rawClassOf(final Type type) |
| 113 | + { |
| 114 | + if (type instanceof SimpleType simpleType) { |
| 115 | + return simpleType.getRawClass(); |
| 116 | + } |
| 117 | + if (type instanceof Class<?> c) { |
| 118 | + return c; |
| 119 | + } |
| 120 | + if (type instanceof ParameterizedType p) { |
| 121 | + final Type raw = p.getRawType(); |
| 122 | + if (raw instanceof Class<?> c) { |
| 123 | + return c; |
| 124 | + } |
| 125 | + } |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + private static ModelConverterContext getModelConverterContext() |
| 130 | + { |
| 131 | + var converters = ModelConverters.getInstance().getConverters().stream() |
| 132 | + .filter(converter -> !(converter instanceof TypedIdsModelConverter)) |
| 133 | + .toList(); |
| 134 | + return new ModelConverterContextImpl(converters); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * this automatically fixes names of `User.Id` which would otherwise become `Id` but we want it to be `UserId` |
| 139 | + */ |
| 140 | + private static String getAutomaticSchemaNameForClass(final Class<?> clazz) |
| 141 | + { |
| 142 | + final Class<?> enclosing = clazz.getEnclosingClass(); |
| 143 | + if (enclosing == null) { |
| 144 | + return clazz.getSimpleName(); |
| 145 | + } |
| 146 | + final String outer = getAutomaticSchemaNameForClass(enclosing); |
| 147 | + final String inner = clazz.getSimpleName(); |
| 148 | + return outer + "_" + inner; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments