Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $Id Management in the generator. With it, it is possible to #1632

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jsonschema2pojo-ant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jsonschema2pojo</artifactId>
<groupId>org.jsonschema2pojo</groupId>
<version>1.2.2-SNAPSHOT</version>
<version>1.2.2-Id-Management</version>
</parent>

<artifactId>jsonschema2pojo-ant</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion jsonschema2pojo-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jsonschema2pojo</artifactId>
<groupId>org.jsonschema2pojo</groupId>
<version>1.2.2-SNAPSHOT</version>
<version>1.2.2-Id-Management</version>
</parent>

<artifactId>jsonschema2pojo-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion jsonschema2pojo-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jsonschema2pojo</artifactId>
<groupId>org.jsonschema2pojo</groupId>
<version>1.2.2-SNAPSHOT</version>
<version>1.2.2-Id-Management</version>
</parent>

<artifactId>jsonschema2pojo-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static void generate(GenerationConfig config, RuleLogger logger) throws I
URL source = sources.next();

if (URLUtil.parseProtocol(source.toString()) == URLProtocol.FILE && URLUtil.getFileFromURL(source).isDirectory()) {
parseRecursive(config, mapper, codeModel, defaultString(config.getTargetPackage()), Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter())));
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);
Expand Down Expand Up @@ -139,6 +140,22 @@ private static void generateRecursive(GenerationConfig config, SchemaMapper mapp
}
}
}
private static void parseRecursive(GenerationConfig config, SchemaMapper mapper, JCodeModel codeModel, String packageName, List<File> schemaFiles) throws IOException {

Collections.sort(schemaFiles, config.getSourceSortOrder().getComparator());

for (File child : schemaFiles) {
if (child.isFile()) {
if (config.getSourceType() == SourceType.JSON || config.getSourceType() == SourceType.YAML) {
// any cached schemas will have ids that are fragments, relative to the previous document (and shouldn't be reused)
mapper.getRuleFactory().getSchemaStore().clearCache();
}
mapper.parse(codeModel, getNodeName(child.toURI().toURL(), config), defaultString(packageName), child.toURI().toURL());
} else {
parseRecursive(config, mapper, codeModel, childQualifiedName(packageName, child.getName()), Arrays.asList(child.listFiles(config.getFileFilter())));
}
}
}

private static String childQualifiedName(String parentQualifiedName, String childSimpleName) {
String safeChildName = childSimpleName.replaceAll(NameHelper.ILLEGAL_CHARACTER_REGEX, "_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.jsonschema2pojo.rules.RuleFactory;
Expand All @@ -37,6 +38,7 @@
* used to create type generation rules for this mapper.
*/
public class SchemaMapper {

private static final JsonNodeFactory NODE_FACTORY = JsonNodeFactory.instance;

private final RuleFactory ruleFactory;
Expand Down Expand Up @@ -89,6 +91,35 @@ public JType generate(JCodeModel codeModel, String className, String packageName

return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, null));

}
/**
* Reads a schema and register schema which may be used after during the generation.
*
* @param codeModel
* the java code-generation context that should be used to
* generated new types
* @param className
* the name of the parent class the represented by this schema
* @param packageName
* the target package that should be used for generated types
* @param schemaUrl
* location of the schema to be used as input
*/
public void parse(JCodeModel codeModel, String className, String packageName, URL schemaUrl) {

try {
ObjectNode schemaNode = readSchema(schemaUrl);
SchemaStore schemaStore = getRuleFactory().getSchemaStore();
Schema schema = new Schema(schemaUrl.toURI(), schemaNode, null);
schemaStore.registerId(schema, schemaUrl.toURI(), ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());

} catch (URISyntaxException e) {
e.printStackTrace();
}


// return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, null));

}

private ObjectNode readSchema(URL schemaUrl) {
Expand Down
Loading