Created
October 14, 2021 15:00
-
-
Save finsterwalder/deecf4399dcceea12eef07cd9af825e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package somepackage; | |
import static org.apache.commons.lang3.StringUtils.isNotBlank; | |
import com.fasterxml.jackson.databind.JavaType; | |
import io.swagger.v3.core.converter.AnnotatedType; | |
import io.swagger.v3.core.converter.ModelConverters; | |
import io.swagger.v3.oas.models.OpenAPI; | |
import io.swagger.v3.oas.models.media.Schema; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import java.util.concurrent.atomic.AtomicLong; | |
import org.springdoc.core.customizers.PropertyCustomizer; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class NoRefPropertyCustomizer implements PropertyCustomizer { | |
private static final AtomicLong REF_ID = new AtomicLong(); | |
@Autowired | |
private final OpenAPI openAPI; | |
public NoRefPropertyCustomizer(final OpenAPI openAPI) { | |
this.openAPI = openAPI; | |
} | |
@Override | |
public Schema customize(Schema schema, AnnotatedType type) { | |
if (schema != null && type != null && type.getCtxAnnotations() != null) { | |
for (Annotation annotation : type.getCtxAnnotations()) { | |
Class<? extends Annotation> annotationType = annotation.annotationType(); | |
if (NoRefSchema.class.equals(annotationType)) { | |
final NoRefSchema noRefSchema = (NoRefSchema) annotation; | |
final Type annotatedType = type.getType(); | |
if (annotatedType instanceof JavaType) { | |
JavaType javaType = (JavaType) annotatedType; | |
final Class<?> rawClass = javaType.getRawClass(); | |
final String $ref = rawClass.getSimpleName() + "_" + REF_ID.incrementAndGet(); | |
final Schema newSchema = getSchema(rawClass); | |
if (isNotBlank(noRefSchema.description())) { | |
newSchema.description(noRefSchema.description()); | |
} | |
if (isNotBlank(noRefSchema.example())) { | |
newSchema.example(noRefSchema.example()); | |
} | |
if (isNotBlank(noRefSchema.defaultValue())) { | |
newSchema.setDefault(noRefSchema.defaultValue()); | |
} | |
if (noRefSchema.deprecated()) { | |
newSchema.deprecated(noRefSchema.deprecated()); | |
} | |
if (noRefSchema.required()) { | |
type.getParent().addRequiredItem(type.getPropertyName()); | |
} | |
openAPI.getComponents().addSchemas($ref, newSchema); | |
schema.$ref($ref); | |
} | |
} | |
} | |
} | |
return schema; | |
} | |
private Schema getSchema(Class aClass) { | |
return ModelConverters.getInstance() | |
.resolveAsResolvedSchema(new AnnotatedType(aClass).resolveAsRef(false)) | |
.schema; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment