Search in sources :

Example 1 with SchemaMapper

use of org.jsonschema2pojo.SchemaMapper in project jsonschema2pojo by joelittlejohn.

the class Example method main.

public static void main(String[] args) throws IOException {
    // BEGIN EXAMPLE
    JCodeModel codeModel = new JCodeModel();
    URL source = Example.class.getResource("/schema/required.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(Files.createTempDirectory("required").toFile());
// END EXAMPLE
}
Also used : DefaultGenerationConfig(org.jsonschema2pojo.DefaultGenerationConfig) RuleFactory(org.jsonschema2pojo.rules.RuleFactory) SchemaStore(org.jsonschema2pojo.SchemaStore) Jackson2Annotator(org.jsonschema2pojo.Jackson2Annotator) SchemaGenerator(org.jsonschema2pojo.SchemaGenerator) JCodeModel(com.sun.codemodel.JCodeModel) URL(java.net.URL) GenerationConfig(org.jsonschema2pojo.GenerationConfig) DefaultGenerationConfig(org.jsonschema2pojo.DefaultGenerationConfig) SchemaMapper(org.jsonschema2pojo.SchemaMapper)

Example 2 with SchemaMapper

use of org.jsonschema2pojo.SchemaMapper in project jsonschema2pojo by joelittlejohn.

the class SelfRefIT method nestedSelfRefsInStringContentWithoutParentFile.

@Test
public void nestedSelfRefsInStringContentWithoutParentFile() throws NoSuchMethodException, ClassNotFoundException, IOException {
    String schemaContents = IOUtils.toString(CodeGenerationHelper.class.getResource("/schema/ref/nestedSelfRefsReadAsString.json"));
    JCodeModel codeModel = new JCodeModel();
    new SchemaMapper().generate(codeModel, "NestedSelfRefsInString", "com.example", schemaContents);
    codeModel.build(schemaRule.getGenerateDir());
    ClassLoader classLoader = schemaRule.compile();
    Class<?> nestedSelfRefs = classLoader.loadClass("com.example.NestedSelfRefsInString");
    assertThat(nestedSelfRefs.getMethod("getThings").getReturnType().getSimpleName(), equalTo("List"));
    Class<?> listEntryType = (Class<?>) ((ParameterizedType) nestedSelfRefs.getMethod("getThings").getGenericReturnType()).getActualTypeArguments()[0];
    assertThat(listEntryType.getName(), equalTo("com.example.Thing"));
    Class<?> thingClass = classLoader.loadClass("com.example.Thing");
    assertThat(thingClass.getMethod("getNamespace").getReturnType().getSimpleName(), equalTo("String"));
    assertThat(thingClass.getMethod("getName").getReturnType().getSimpleName(), equalTo("String"));
    assertThat(thingClass.getMethod("getVersion").getReturnType().getSimpleName(), equalTo("String"));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CodeGenerationHelper(org.jsonschema2pojo.integration.util.CodeGenerationHelper) BeforeClass(org.junit.BeforeClass) JCodeModel(com.sun.codemodel.JCodeModel) SchemaMapper(org.jsonschema2pojo.SchemaMapper) Test(org.junit.Test)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)2 SchemaMapper (org.jsonschema2pojo.SchemaMapper)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 URL (java.net.URL)1 DefaultGenerationConfig (org.jsonschema2pojo.DefaultGenerationConfig)1 GenerationConfig (org.jsonschema2pojo.GenerationConfig)1 Jackson2Annotator (org.jsonschema2pojo.Jackson2Annotator)1 SchemaGenerator (org.jsonschema2pojo.SchemaGenerator)1 SchemaStore (org.jsonschema2pojo.SchemaStore)1 CodeGenerationHelper (org.jsonschema2pojo.integration.util.CodeGenerationHelper)1 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1