use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class FragmentRefIT method refToInnerFragmentThatHasRefToAnotherFragmentWithoutParentFile.
@Test
public void refToInnerFragmentThatHasRefToAnotherFragmentWithoutParentFile() throws IOException {
JCodeModel codeModel = new JCodeModel();
JsonNode schema = new ObjectMapper().readTree("{\n" + " \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" + " \"title\": \"Inbox Item Datalake DTO\",\n" + " \"definitions\": {\n" + " \"PropertyA\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"value\": {\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"PropertyB\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"data\": {\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"$ref\": \"#/definitions/PropertyA\"\n" + " },\n" + " \"default\": []\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"properties\": {\n" + " \"FinalProperty\": {\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"$ref\": \"#/definitions/PropertyB\"\n" + " },\n" + " \"default\": []\n" + " }\n" + " }\n" + "}");
JPackage p = codeModel._package("com.example");
new RuleFactory().getSchemaRule().apply("Example", schema, null, p, new Schema(null, schema, null));
}
use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class ArrayRuleTest method arrayOfPrimitivesProducesCollectionOfWrapperTypes.
@Test
public void arrayOfPrimitivesProducesCollectionOfWrapperTypes() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "number");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.FALSE);
propertyNode.set("items", itemsNode);
Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
when(config.isUsePrimitives()).thenReturn(true);
when(config.isUseDoubleNumbers()).thenReturn(true);
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Double.class.getName()));
}
use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class ArrayRuleTest method arrayDefaultsToNonUnique.
@Test
public void arrayDefaultsToNonUnique() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "boolean");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.FALSE);
propertyNode.set("items", itemsNode);
Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/defaultArray"));
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
}
use of com.sun.codemodel.JCodeModel 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());
}
}
use of com.sun.codemodel.JCodeModel in project raml-for-jax-rs by mulesoft-labs.
the class JAXBHelper method generateClassesFromXmlSchemas.
private static List<JDefinedClass> generateClassesFromXmlSchemas(String pack, JCodeModel codeModel, File schemaFile) throws GenerationException {
try {
ArrayList<JDefinedClass> classList = new ArrayList<JDefinedClass>();
ArrayList<String> argList = new ArrayList<>();
argList.add("-mark-generated");
argList.add("-p");
argList.add(pack);
argList.add(schemaFile.getAbsolutePath());
String[] args = argList.toArray(new String[argList.size()]);
final Options opt = new Options();
opt.setSchemaLanguage(Language.XMLSCHEMA);
opt.parseArguments(args);
ErrorReceiver receiver = new ErrorReceiverFilter() {
@Override
public void info(SAXParseException exception) {
if (opt.verbose)
super.info(exception);
}
@Override
public void warning(SAXParseException exception) {
if (!opt.quiet)
super.warning(exception);
}
};
Model model = ModelLoader.load(opt, codeModel, receiver);
Outline outline = model.generateCode(opt, receiver);
for (ClassOutline co : outline.getClasses()) {
JDefinedClass cl = co.implClass;
if (cl.outer() == null) {
classList.add(cl);
}
}
return classList;
} catch (Exception e) {
throw new GenerationException(e);
}
}
Aggregations