Skip to content

Commit

Permalink
feat: add subtype resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenWickner authored and takanuva15 committed Nov 5, 2023
1 parent dd2bddd commit 4d49964
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/utils/GeneralConfigModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.victools.jsonschema.generator.*;
import com.github.victools.jsonschema.generator.Module;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.ScanResult;
import java.util.List;
import java.util.stream.Collectors;

public class GeneralConfigModule implements Module {
@Override
public void applyToConfigBuilder(SchemaGeneratorConfigBuilder builder) {
InsertSchemaPropsProvider descriptionProvider = new InsertSchemaPropsProvider();
builder.forTypesInGeneral()
.withSubtypeResolver(new ClassGraphSubtypeResolver())
.withCustomDefinitionProvider(descriptionProvider)
.withTypeAttributeOverride(descriptionProvider);
}
Expand Down Expand Up @@ -38,4 +44,57 @@ public void resetAfterSchemaGenerationFinished() {
this.mainType = null;
}
}

/**
* Simple implementation of a reflection based subtype resolver, considering only subtypes from a certain package.
*/
private class ClassGraphSubtypeResolver implements SubtypeResolver {

private final ClassGraph classGraphConfig;
private ScanResult scanResult;

ClassGraphSubtypeResolver() {
this.classGraphConfig = new ClassGraph()
.enableClassInfo()
.enableInterClassDependencies()
// in this example, only consider a certain set of potential subtypes
.acceptPackages("org.example.model");
}

private ScanResult getScanResult() {
if (this.scanResult == null) {
this.scanResult = this.classGraphConfig.scan();
}
return this.scanResult;
}

@Override
public void resetAfterSchemaGenerationFinished() {
if (this.scanResult != null) {
this.scanResult.close();
this.scanResult = null;
}
}

@Override
public List<ResolvedType> findSubtypes(ResolvedType declaredType, SchemaGenerationContext context) {
if (declaredType.getErasedType() == Object.class) {
return null;
}
ClassInfoList subtypes;
if (declaredType.isInterface()) {
subtypes = this.getScanResult().getClassesImplementing(declaredType.getErasedType());
} else {
subtypes = this.getScanResult().getSubclasses(declaredType.getErasedType());
}
if (!subtypes.isEmpty()) {
TypeContext typeContext = context.getTypeContext();
return subtypes.loadClasses(true)
.stream()
.map(subclass -> typeContext.resolveSubtype(declaredType, subclass))
.collect(Collectors.toList());
}
return null;
}
}
}

0 comments on commit 4d49964

Please sign in to comment.