Search in sources :

Example 6 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class SchemaMapperTest method generateCreatesSchemaFromExampleJsonWhenInJsonMode.

@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"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JCodeModel(com.sun.codemodel.JCodeModel) URL(java.net.URL) Test(org.junit.Test)

Example 7 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class Jsonschema2Pojo method generate.

/**
     * Reads the contents of the given source and initiates schema generation.
     *
     * @param config
     *            the configuration options (including source and target paths,
     *            and other behavioural options) that will control code
     *            generation
     * @throws FileNotFoundException
     *             if the source path is not found
     * @throws IOException
     *             if the application is unable to read data from the source
     */
public static void generate(GenerationConfig config) throws IOException {
    Annotator annotator = getAnnotator(config);
    RuleFactory ruleFactory = createRuleFactory(config);
    ruleFactory.setAnnotator(annotator);
    ruleFactory.setGenerationConfig(config);
    SchemaMapper mapper = new SchemaMapper(ruleFactory, new SchemaGenerator());
    JCodeModel codeModel = new JCodeModel();
    if (config.isRemoveOldOutput()) {
        removeOldOutput(config.getTargetDirectory());
    }
    for (Iterator<URL> sources = config.getSource(); sources.hasNext(); ) {
        URL source = sources.next();
        if (URLUtil.parseProtocol(source.toString()) == URLProtocol.FILE && URLUtil.getFileFromURL(source).isDirectory()) {
            generateRecursive(config, mapper, codeModel, defaultString(config.getTargetPackage()), Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter())));
        } else {
            mapper.generate(codeModel, getNodeName(source, config), defaultString(config.getTargetPackage()), source);
        }
    }
    if (config.getTargetDirectory().exists() || config.getTargetDirectory().mkdirs()) {
        CodeWriter sourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        CodeWriter resourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        codeModel.build(sourcesWriter, resourcesWriter);
    } else {
        throw new GenerationException("Could not create or access target directory " + config.getTargetDirectory().getAbsolutePath());
    }
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) GenerationException(org.jsonschema2pojo.exception.GenerationException) JCodeModel(com.sun.codemodel.JCodeModel) CodeWriter(com.sun.codemodel.CodeWriter) URL(java.net.URL)

Example 8 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class FragmentRefIT method refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile.

@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));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 9 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class FragmentRefIT method selfRefWithoutParentFile.

@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));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)9 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)9 JPackage (com.sun.codemodel.JPackage)6 Test (org.junit.Test)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 URL (java.net.URL)4 SchemaRule (org.jsonschema2pojo.rules.SchemaRule)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 File (java.io.File)2 Schema (org.jsonschema2pojo.Schema)2 CodeWriter (com.sun.codemodel.CodeWriter)1 DefaultGenerationConfig (org.jsonschema2pojo.DefaultGenerationConfig)1 GenerationConfig (org.jsonschema2pojo.GenerationConfig)1 Jackson2Annotator (org.jsonschema2pojo.Jackson2Annotator)1 SchemaGenerator (org.jsonschema2pojo.SchemaGenerator)1 SchemaMapper (org.jsonschema2pojo.SchemaMapper)1 SchemaStore (org.jsonschema2pojo.SchemaStore)1 GenerationException (org.jsonschema2pojo.exception.GenerationException)1