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 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, schema); assertThat(result, is(sameInstance(previouslyGeneratedType))); }
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"); }
public APXCustomRuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore) { super(generationConfig, annotator, schemaStore); }
/** * 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(); }
/** * Create a new rule factory with the given generation config options. * * @param generationConfig * The generation config options for type generation. These * config options will influence the java code generated by rules * created by this factory. * @param annotator * the annotator used to mark up Java types with any annotations * that are required to build JSON compatible types * @param schemaStore * the object used by this factory to get and store schemas */ public RuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore) { this.generationConfig = generationConfig; this.annotator = annotator; this.schemaStore = schemaStore; this.nameHelper = new NameHelper(generationConfig); }
@Test public void generationConfigIsReturned() { GenerationConfig mockGenerationConfig = mock(GenerationConfig.class); RuleFactory ruleFactory = new RuleFactory(mockGenerationConfig, new NoopAnnotator(), new SchemaStore()); assertThat(ruleFactory.getGenerationConfig(), is(sameInstance(mockGenerationConfig))); }
@Test public void schemaStoreIsReturned() { SchemaStore mockSchemaStore = mock(SchemaStore.class); RuleFactory ruleFactory = new RuleFactory(new DefaultGenerationConfig(), new NoopAnnotator(), mockSchemaStore); assertThat(ruleFactory.getSchemaStore(), is(sameInstance(mockSchemaStore))); }
@Test public void refsToOtherSchemasAreLoaded() throws URISyntaxException, JClassAlreadyExistsException { URI schemaUri = getClass().getResource("/schema/address.json").toURI(); ObjectNode schemaWithRef = new ObjectMapper().createObjectNode(); schemaWithRef.put("$ref", schemaUri.toString()); JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME); final GenerationConfig mockGenerationConfig = mock(GenerationConfig.class); when(mockGenerationConfig.getRefFragmentPathDelimiters()).thenReturn("#/."); TypeRule mockTypeRule = mock(TypeRule.class); when(mockRuleFactory.getTypeRule()).thenReturn(mockTypeRule); when(mockRuleFactory.getSchemaStore()).thenReturn(new SchemaStore()); when(mockRuleFactory.getGenerationConfig()).thenReturn(mockGenerationConfig); ArgumentCaptor<JsonNode> captureJsonNode = ArgumentCaptor.forClass(JsonNode.class); ArgumentCaptor<Schema> captureSchema = ArgumentCaptor.forClass(Schema.class); rule.apply(NODE_NAME, schemaWithRef, jclass, null); verify(mockTypeRule).apply(eq(NODE_NAME), captureJsonNode.capture(), eq(jclass.getPackage()), captureSchema.capture()); assertThat(captureSchema.getValue().getId(), is(equalTo(schemaUri))); assertThat(captureSchema.getValue().getContent(), is(equalTo(captureJsonNode.getValue()))); assertThat(captureJsonNode.getValue().get("description").asText(), is(equalTo("An Address following the convention of http://microformats.org/wiki/hcard"))); }
/** * Create a rule factory with the default generation config options. * * @see DefaultGenerationConfig */ public RuleFactory() { this(new DefaultGenerationConfig(), new Jackson2Annotator(new DefaultGenerationConfig()), new SchemaStore()); }
/** * Gets the store that finds and saves JSON schemas * * @return a store that finds and caches schema objects during type * generation. */ public SchemaStore getSchemaStore() { return schemaStore; }
/** * The object used by this factory to get and store schemas * * @param schemaStore * schema store */ public void setSchemaStore(final SchemaStore schemaStore) { this.schemaStore = schemaStore; }