private ObjectNode buildDefinitionsAndResolveReferences(String referenceKeyPrefix, DefinitionKey mainSchemaKey) {
        final ObjectNode definitionsNode = this.config.createObjectNode();

        final AtomicBoolean considerOnlyDirectReferences = new AtomicBoolean(false);
        Predicate<DefinitionKey> shouldProduceDefinition = this.getShouldProduceDefinitionCheck(mainSchemaKey, considerOnlyDirectReferences);
        DefinitionCollectionDetails definitionCollectionDetails = new DefinitionCollectionDetails(mainSchemaKey, referenceKeyPrefix,
                shouldProduceDefinition, definitionsNode);

        Map<DefinitionKey, String> baseReferenceKeys = this.getReferenceKeys(mainSchemaKey, shouldProduceDefinition);
        considerOnlyDirectReferences.set(true);

        for (Map.Entry<DefinitionKey, String> baseReferenceKey : baseReferenceKeys.entrySet()) {
            DefinitionKey definitionKey = baseReferenceKey.getKey();
            List<ObjectNode> references = this.generationContext.getReferences(definitionKey);
            List<ObjectNode> nullableReferences = this.generationContext.getNullableReferences(definitionKey);
            final String referenceKey = this.updateReferences(references, definitionCollectionDetails, baseReferenceKey);
            if (!nullableReferences.isEmpty()) {
                updateNullableReferences(nullableReferences, definitionCollectionDetails, referenceKey, baseReferenceKey);
            }
        }
        definitionsNode.forEach(node -> this.schemaNodes.add((ObjectNode) node));
        return definitionsNode;
    }
Finalisation Step: collect the entries for the generated schema's "definitions" and ensure that all references are either pointing to the
appropriate definition or contain the respective (sub) schema directly inline.

@param referenceKeyPrefix designated prefix to the entries in the returned definitions node (i.e., on {@link SchemaKeyword#TAG_REF} values)
@param mainSchemaKey definition key identifying the main type for which createSchemaReference() was invoked
@return node representing the main schema's "definitions" (may be empty)
jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaBuilder.java