Search in sources :

Example 1 with SchemaStore

use of org.jsonschema2pojo.SchemaStore 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 SchemaStore

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

the class MinLengthMaxLengthRuleTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    rule = new MinLengthMaxLengthRule(new RuleFactory(config, new NoopAnnotator(), new SchemaStore()));
    when(config.isUseJakartaValidation()).thenReturn(useJakartaValidation);
}
Also used : SchemaStore(org.jsonschema2pojo.SchemaStore) NoopAnnotator(org.jsonschema2pojo.NoopAnnotator) Before(org.junit.Before)

Example 3 with SchemaStore

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

the class PatternRuleTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    rule = new PatternRule(new RuleFactory(config, new NoopAnnotator(), new SchemaStore()));
    when(config.isUseJakartaValidation()).thenReturn(useJakartaValidation);
}
Also used : SchemaStore(org.jsonschema2pojo.SchemaStore) NoopAnnotator(org.jsonschema2pojo.NoopAnnotator) Before(org.junit.Before)

Example 4 with SchemaStore

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

the class SchemaRuleTest method existingTypeIsUsedWhenTypeIsAlreadyGenerated.

@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, null, schema);
    assertThat(result, is(sameInstance(previouslyGeneratedType)));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SchemaStore(org.jsonschema2pojo.SchemaStore) Schema(org.jsonschema2pojo.Schema) URI(java.net.URI) JType(com.sun.codemodel.JType) GenerationConfig(org.jsonschema2pojo.GenerationConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with SchemaStore

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

the class SchemaRuleTest method refsToOtherSchemasAreLoaded.

@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, null, jclass, null);
    verify(mockTypeRule).apply(eq("address"), captureJsonNode.capture(), any(), 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")));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JDefinedClass(com.sun.codemodel.JDefinedClass) SchemaStore(org.jsonschema2pojo.SchemaStore) Schema(org.jsonschema2pojo.Schema) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GenerationConfig(org.jsonschema2pojo.GenerationConfig) Test(org.junit.Test)

Aggregations

SchemaStore (org.jsonschema2pojo.SchemaStore)12 NoopAnnotator (org.jsonschema2pojo.NoopAnnotator)9 Before (org.junit.Before)6 GenerationConfig (org.jsonschema2pojo.GenerationConfig)5 Test (org.junit.Test)5 DefaultGenerationConfig (org.jsonschema2pojo.DefaultGenerationConfig)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 JCodeModel (com.sun.codemodel.JCodeModel)2 URI (java.net.URI)2 RuleLogger (org.jsonschema2pojo.RuleLogger)2 Schema (org.jsonschema2pojo.Schema)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 JType (com.sun.codemodel.JType)1 URL (java.net.URL)1 Jackson2Annotator (org.jsonschema2pojo.Jackson2Annotator)1 SchemaGenerator (org.jsonschema2pojo.SchemaGenerator)1 SchemaMapper (org.jsonschema2pojo.SchemaMapper)1 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)1