@Override
    public JType apply(String nodeName, JsonNode schemaNode, JsonNode parent, JClassContainer generatableType, Schema schema) {

        if (schemaNode.has("$ref")) {
            final String nameFromRef = nameFromRef(schemaNode.get("$ref").asText());

            schema = ruleFactory.getSchemaStore().create(schema, schemaNode.get("$ref").asText(), ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());
            schemaNode = schema.getContent();

            if (schema.isGenerated()) {
                return schema.getJavaType();
            }

            return apply(nameFromRef != null ? nameFromRef : nodeName, schemaNode, parent, generatableType, schema);
        }

        JType javaType;
        if (schemaNode.has("enum")) {
            javaType = ruleFactory.getEnumRule().apply(nodeName, schemaNode, parent, generatableType, schema);
        } else {
            javaType = ruleFactory.getTypeRule().apply(nodeName, schemaNode, parent, generatableType.getPackage(), schema);
        }
        schema.setJavaTypeIfEmpty(javaType);

        return javaType;
    }
@Test
    public void existingTypeIsUsedWhenTypeIsAlreadyGenerated() throws URISyntaxException {

        JType previouslyGeneratedType = mock(JType.class);

        URI schemaUri = getClass().getResource("/schema/address.json").toURI();

        SchemaStore schemaStore = new SchemaStore();
        Schema schema = schemaStore.create(schemaUri, "#/.");
        schema.setJavaType(previouslyGeneratedType);

        final GenerationConfig mockGenerationConfig = mock(GenerationConfig.class);
        when(mockGenerationConfig.getRefFragmentPathDelimiters()).thenReturn("#/.");

        when(mockRuleFactory.getSchemaStore()).thenReturn(schemaStore);
        when(mockRuleFactory.getGenerationConfig()).thenReturn(mockGenerationConfig);

        ObjectNode schemaNode = new ObjectMapper().createObjectNode();
        schemaNode.put("$ref", schemaUri.toString());

        JType result = rule.apply(NODE_NAME, schemaNode, null,null, schema);

        assertThat(result, is(sameInstance(previouslyGeneratedType)));

    }