mbknor / mbknor-jackson-jsonSchema

Generate JSON Schema with Polymorphism using Jackson annotations
MIT License
232 stars 75 forks source link

Calling `JsonSchemaConfig.builder()` scans the entire classpath #182

Open stellalie opened 1 year ago

stellalie commented 1 year ago

This is because calling JsonSchemaConfig.builder() automatically sets the SubclassesResolver(null) and which in turn trigger the entire classpath scan to build class graph, such

public SubclassesResolver(ClassGraph classGraph) {
        if (classGraph == null) {
            log.debug("Entire classpath will be scanned because SubclassesResolver is not configured. See https://github.com/mbknor/mbknor-jackson-jsonSchema#subclass-resolving-using-reflection");
            classGraph = new ClassGraph();
        }

        this.scanResult = classGraph.enableClassInfo().scan();
    }

This is prob the side effect on generated code from scala to java - but I haven't dig deeper

This takes up a lot of memory and is a waste of resources esp. if we are specifying the packages/classes to scan. In the code below, we are already scanning the entire classpath at JsonSchemaConfig.builder() before setting limiting the packages to scan subclassesResolver(..) e.g


    val config = JsonSchemaConfig.builder()
        .subclassesResolver(SubclassesResolver(listOf(ENTITY_PACKAGES_TO_SCAN), listOf()))
         .....
        .build()```