@Override @SuppressWarnings("unchecked") public Class<? extends RuleFactory> getCustomRuleFactory() { if (isNotBlank(customRuleFactory)) { try { return (Class<? extends RuleFactory>) Class.forName(customRuleFactory); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } } else { return RuleFactory.class; } }
@SuppressWarnings("unchecked") public void setCustomRuleFactory(String customRuleFactory) { if (isNotBlank(customRuleFactory)) { try { this.customRuleFactory = (Class<? extends RuleFactory>) Class.forName(customRuleFactory); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } } else { this.customRuleFactory = RuleFactory.class; } }
@Test public void selfRefWithoutParentFile() throws IOException { JCodeModel codeModel = new JCodeModel(); JsonNode schema = new ObjectMapper().readTree("{\"type\":\"object\", \"properties\":{\"a\":{\"$ref\":\"#/b\"}}, \"b\":\"string\"}"); JPackage p = codeModel._package("com.example"); new RuleFactory().getSchemaRule().apply("Example", schema, p, new Schema(null, schema, schema)); }
@Test public void refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile() throws IOException, ClassNotFoundException { JCodeModel codeModel = new JCodeModel(); JsonNode schema = new ObjectMapper().readTree("{\n" + " \"type\": \"object\",\n" + " \"definitions\": {\n" + " \"location\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"cat\": {\n" + " \"$ref\": \"#/definitions/cat\"\n" + " }\n" + " }\n" + " },\n" + " \"cat\": {\n" + " \"type\": \"number\"\n" + " }\n" + " },\n" + " \"properties\": {\n" + " \"location\": {\n" + " \"$ref\": \"#/definitions/location\"\n" + " }\n" + " }\n" + "}"); JPackage p = codeModel._package("com.example"); new RuleFactory().getSchemaRule().apply("Example", schema, p, new Schema(null, schema, schema)); }
private SchemaMapper createSchemaMapper() { final RuleFactory ruleFactory = new RuleFactory(); ruleFactory.setAnnotator(new Jackson2Annotator() { @Override public boolean isAdditionalPropertiesSupported() { return false; } }); ruleFactory.setGenerationConfig(config); return new SchemaMapper(ruleFactory, new SchemaGenerator()); }
@Override public Class<? extends RuleFactory> getCustomRuleFactory() { return RuleFactory.class; }
public static void main(String[] args) throws IOException { // BEGIN EXAMPLE JCodeModel codeModel = new JCodeModel(); URL source = new URL("file:///path/to/my/schema.json"); GenerationConfig config = new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding method return true; } }; SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator()); mapper.generate(codeModel, "ClassName", "com.example", source); codeModel.build(new File("output")); // END EXAMPLE }
@Test public void generateReadsSchemaAsObject() throws IOException { final SchemaRule mockSchemaRule = mock(SchemaRule.class); final RuleFactory mockRuleFactory = mock(RuleFactory.class); when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule); when(mockRuleFactory.getGenerationConfig()).thenReturn(new DefaultGenerationConfig()); URL schemaContent = this.getClass().getResource("/schema/address.json"); new SchemaMapper(mockRuleFactory, new SchemaGenerator()).generate(new JCodeModel(), "Address", "com.example.package", schemaContent); ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class); ArgumentCaptor<JsonNode> captureNode = ArgumentCaptor.forClass(JsonNode.class); verify(mockSchemaRule).apply(eq("Address"), captureNode.capture(), capturePackage.capture(), Mockito.isA(Schema.class)); assertThat(capturePackage.getValue().name(), is("com.example.package")); assertThat(captureNode.getValue(), is(notNullValue())); }
@Test public void generateCreatesSchemaFromSchemaAsStringInput() throws IOException { String schemaContent = IOUtils.toString(this.getClass().getResourceAsStream("/schema/address.json")); final SchemaRule mockSchemaRule = mock(SchemaRule.class); final RuleFactory mockRuleFactory = mock(RuleFactory.class); when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule); when(mockRuleFactory.getGenerationConfig()).thenReturn(new DefaultGenerationConfig()); new SchemaMapper(mockRuleFactory, new SchemaGenerator()).generate(new JCodeModel(), "Address", "com.example.package", schemaContent); ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class); ArgumentCaptor<JsonNode> captureNode = ArgumentCaptor.forClass(JsonNode.class); verify(mockSchemaRule).apply(eq("Address"), captureNode.capture(), capturePackage.capture(), Mockito.isA(Schema.class)); assertThat(capturePackage.getValue().name(), is("com.example.package")); assertThat(captureNode.getValue(), is(notNullValue())); }
@Override public Class<? extends RuleFactory> getCustomRuleFactory() { return customRuleFactory; }
protected JCodeModel getCodegenModel(String schema, String targetpackage, final String sourcetype, final String annotationstyle, final boolean usedoublenumbers, final boolean includeaccessors, final boolean includeadditionalproperties, final String propertyworddelimiters, String classname) throws IOException { JCodeModel codeModel = new JCodeModel(); GenerationConfig config = new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding method return true; } @Override public SourceType getSourceType() { return SourceType.valueOf(sourcetype.toUpperCase()); } @Override public AnnotationStyle getAnnotationStyle() { return AnnotationStyle.valueOf(annotationstyle.toUpperCase()); } @Override public boolean isUseDoubleNumbers() { return usedoublenumbers; } @Override public boolean isIncludeAccessors() { return includeaccessors; } @Override public boolean isIncludeAdditionalProperties() { return includeadditionalproperties; } @Override public char[] getPropertyWordDelimiters() { return propertyworddelimiters.replace(" ", "").toCharArray(); } }; AnnotatorFactory factory = new AnnotatorFactory(config); CompositeAnnotator annotator = factory.getAnnotator(factory.getAnnotator(config.getAnnotationStyle()), factory.getAnnotator(config.getCustomAnnotator())); SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, annotator, new SchemaStore()), new SchemaGenerator()); mapper.generate(codeModel, classname, targetpackage, schema); return codeModel; }
public void createModel(File jsonFile, String modelName) throws IOException { //CapFirst for java classes modelName = modelName.substring(0, 1).toUpperCase() + modelName.substring(1); System.out.println("Model name :"+modelName); JCodeModel codeModel = new JCodeModel(); GenerationConfig config = new DefaultGenerationConfig() { @Override public boolean isIncludeConstructors() { return true; } @Override public boolean isUseDoubleNumbers() { return true; } @Override public boolean isUsePrimitives() { return true; } @Override public boolean isIncludeToString() { return false; } @Override public boolean isIncludeHashcodeAndEquals() { return false; } @Override public boolean isIncludeAdditionalProperties() { return false; } @Override public Class<? extends RuleFactory> getCustomRuleFactory() { return APXCustomRuleFactory.class; } }; SchemaMapper mapper = new SchemaMapper(new APXCustomRuleFactory(config, new ORMLiteAnotator(), new SchemaStore()), new SchemaGenerator()); JType m = mapper.generate(codeModel, modelName, PACKAGE_NAME + ".models", jsonFile.toURI().toURL()); File f = new File(PROJECT_SRC); codeModel.build(f); System.out.print("Model created at :"); System.out.println(m.fullName().replace('.', File.separatorChar) + ".java"); }
/** * Generates classes based on json/jsonschema. * * @return class name * @throws IOException */ public String generate() throws IOException { String schemaPath = this.config.getJsonSchemaPath(); if (schemas.containsKey(schemaPath)){ LOG.info("Schema already exists " + schemaPath); return schemas.get(schemaPath); } JCodeModel codeModel = new JCodeModel(); URL source = new File(config.getInputPath()).toURI().toURL(); GenerationConfig generationConfig = new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding metho return true; } @Override public SourceType getSourceType() { if (JsonPath.from(source).get("$schema") != null) { return SourceType.JSONSCHEMA; } return SourceType.JSON; } @Override public boolean isUseLongIntegers() { return true; } @Override public boolean isUseCommonsLang3() { return true; } }; SchemaMapper mapper = new SchemaMapper( new RuleFactory(generationConfig, new GsonAnnotator(), new SchemaStore()), new SchemaGenerator()); mapper.generate(codeModel, getClassName(), config.getPackageName(), source); codeModel.build(new File(config.getOutputPath())); schemas.put(schemaPath, getClassName()); return getClassName(); }
public SoeDefaultRule(RuleFactory ruleFactory) { this.ruleFactory = ruleFactory; }
protected SoeTypeRule(RuleFactory ruleFactory) { this.ruleFactory = ruleFactory; }
@Test public void generateCreatesSchemaFromExampleJsonWhenInJsonMode() throws IOException { URL schemaContent = this.getClass().getResource("/schema/address.json"); ObjectNode schemaNode = JsonNodeFactory.instance.objectNode(); final SchemaRule mockSchemaRule = mock(SchemaRule.class); final GenerationConfig mockGenerationConfig = mock(GenerationConfig.class); when(mockGenerationConfig.getSourceType()).thenReturn(SourceType.JSON); final SchemaGenerator mockSchemaGenerator = mock(SchemaGenerator.class); when(mockSchemaGenerator.schemaFromExample(schemaContent)).thenReturn(schemaNode); final RuleFactory mockRuleFactory = mock(RuleFactory.class); when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule); when(mockRuleFactory.getGenerationConfig()).thenReturn(mockGenerationConfig); new SchemaMapper(mockRuleFactory, mockSchemaGenerator).generate(new JCodeModel(), "Address", "com.example.package", schemaContent); ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class); verify(mockSchemaRule).apply(eq("Address"), eq(schemaNode), capturePackage.capture(), Mockito.isA(Schema.class)); assertThat(capturePackage.getValue().name(), is("com.example.package")); }
@Test public void generateCreatesSchemaFromExampleJSONAsStringInput() throws IOException { String jsonContent = IOUtils.toString(this.getClass().getResourceAsStream("/example-json/user.json")); ObjectNode schemaNode = JsonNodeFactory.instance.objectNode(); final SchemaRule mockSchemaRule = mock(SchemaRule.class); final GenerationConfig mockGenerationConfig = mock(GenerationConfig.class); when(mockGenerationConfig.getSourceType()).thenReturn(SourceType.JSON); final SchemaGenerator mockSchemaGenerator = mock(SchemaGenerator.class); when(mockSchemaGenerator.schemaFromExample(new ObjectMapper().readTree(jsonContent))).thenReturn(schemaNode); final RuleFactory mockRuleFactory = mock(RuleFactory.class); when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule); when(mockRuleFactory.getGenerationConfig()).thenReturn(mockGenerationConfig); new SchemaMapper(mockRuleFactory, mockSchemaGenerator).generate(new JCodeModel(), "User", "com.example.package", jsonContent); ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class); verify(mockSchemaRule).apply(eq("User"), eq(schemaNode), capturePackage.capture(), Mockito.isA(Schema.class)); assertThat(capturePackage.getValue().name(), is("com.example.package")); }
/** * Gets the 'customRuleFactory' configuration option. * * @return An Rule Factory that will be used for the creation of generation * rules. */ Class<? extends RuleFactory> getCustomRuleFactory();
/** * Create a schema mapper with the given {@link RuleFactory}. * * @param ruleFactory * A factory used by this mapper to create Java type generation * rules. * @param schemaGenerator * the generator that this mapper will use if the config dictates * that the input documents are plain json (not json schema) */ public SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator) { this.ruleFactory = ruleFactory; this.schemaGenerator = schemaGenerator; }
/** * Create a schema mapper with the default {@link RuleFactory} * implementation. * * @see RuleFactory */ public SchemaMapper() { this(new RuleFactory(), new SchemaGenerator()); }