@Test public void enumAsRootIsGeneratedCorrectly() throws JClassAlreadyExistsException { ObjectNode schemaContent = new ObjectMapper().createObjectNode(); ObjectNode enumNode = schemaContent.objectNode(); enumNode.put("type", "string"); schemaContent.set("enum", enumNode); JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME); Schema schema = mock(Schema.class); when(schema.getContent()).thenReturn(schemaContent); when(schema.deriveChildSchema(any())).thenReturn(schema); schema.setJavaTypeIfEmpty(jclass); EnumRule enumRule = mock(EnumRule.class); when(mockRuleFactory.getEnumRule()).thenReturn(enumRule); when(enumRule.apply(NODE_NAME, enumNode, null, jclass, schema)).thenReturn(jclass); rule.apply(NODE_NAME, schemaContent, null, jclass, schema); verify(enumRule).apply(NODE_NAME, schemaContent, null, jclass, schema); verify(schema, atLeastOnce()).setJavaTypeIfEmpty(jclass); }
@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); } schema = schema.deriveChildSchema(schemaNode); 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; }
SchemaRule implements Rule<JClassContainer, JType> { @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); } schema = schema.deriveChildSchema(schemaNode); 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; } }
SchemaRule implements Rule<JClassContainer, JType> { @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); } schema = schema.deriveChildSchema(schemaNode); 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; } protected  SchemaRule(RuleFactory ruleFactory); }
SchemaRule implements Rule<JClassContainer, JType> { @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); } schema = schema.deriveChildSchema(schemaNode); 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; } protected  SchemaRule(RuleFactory ruleFactory); @Override JType apply(String nodeName, JsonNode schemaNode, JsonNode parent, JClassContainer generatableType, Schema schema); }
SchemaRule implements Rule<JClassContainer, JType> { @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); } schema = schema.deriveChildSchema(schemaNode); 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; } protected  SchemaRule(RuleFactory ruleFactory); @Override JType apply(String nodeName, JsonNode schemaNode, JsonNode parent, JClassContainer generatableType, Schema schema);  }