This is an automated email from the ASF dual-hosted git repository. rmannibucau pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push: new e6316ce9 [JOHNZON-387] better modelisation of ref in pojo generator e6316ce9 is described below commit e6316ce90b60e6c9e5f79b6d6dc390aa718e6512 Author: Romain Manni-Bucau <rmannibu...@gmail.com> AuthorDate: Mon Aug 8 15:01:53 2022 +0200 [JOHNZON-387] better modelisation of ref in pojo generator --- .../jsonschema/generator/PojoGenerator.java | 65 +++++++++++++++++++--- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/generator/PojoGenerator.java b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/generator/PojoGenerator.java index d7b161ad..7c3a9a63 100644 --- a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/generator/PojoGenerator.java +++ b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/generator/PojoGenerator.java @@ -35,6 +35,7 @@ import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; +import java.util.function.Function; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -190,7 +191,10 @@ public class PojoGenerator { * @param ref the reference to resolve. * @return the reference class name if resolved else null. */ - protected String onRef(final String ref) { + protected String onRef(final Ref ref) { + if (configuration.getOnRef() != null) { + return configuration.getOnRef().apply(ref); + } return null; // todo: check if already in nested for ex } @@ -205,7 +209,7 @@ public class PojoGenerator { protected String asType(final String javaName, final JsonObject schema, final boolean required) { final JsonValue ref = schema.get("$ref"); if (ref != null && ref.getValueType() == JsonValue.ValueType.STRING) { - final String name = onRef(JsonString.class.cast(ref).getString()); + final String name = onRef(new Ref(JsonString.class.cast(ref).getString(), imports, attributes, nested)); if (name != null) { return name; } @@ -411,11 +415,12 @@ public class PojoGenerator { } } else if (hasProperties) { final String className = configuration.getClassName() + Character.toUpperCase(javaName.charAt(0)) + javaName.substring(1); - nested.putAll(new PojoGenerator(new PojoConfiguration() + nested.putAll(newSubPojoGenerator(new PojoConfiguration() .setPackageName(configuration.getPackageName()) .setClassName(className) .setAddJsonbProperty(configuration.isAddJsonbProperty()) - .setAddAllArgsConstructor(configuration.isAddAllArgsConstructor())) + .setAddAllArgsConstructor(configuration.isAddAllArgsConstructor()) + .setOnRef(configuration.getOnRef())) .visitSchema(schema) .generate()); return className; @@ -425,10 +430,14 @@ public class PojoGenerator { return JsonObject.class.getSimpleName(); } + protected PojoGenerator newSubPojoGenerator(final PojoConfiguration pojoConfiguration) { + return new PojoGenerator(pojoConfiguration); + } + protected String onItemSchema(final String javaName, final JsonObject schema) { final JsonValue ref = schema.get("$ref"); if (ref != null && ref.getValueType() == JsonValue.ValueType.STRING) { - final String name = onRef(JsonString.class.cast(ref).getString()); + final String name = onRef(new Ref(JsonString.class.cast(ref).getString(), imports, attributes, nested)); if (name != null) { return name; } @@ -446,11 +455,12 @@ public class PojoGenerator { throw new IllegalStateException("Array of array unsupported"); case "object": final String className = configuration.getClassName() + Character.toUpperCase(javaName.charAt(0)) + javaName.substring(1); - nested.putAll(new PojoGenerator(new PojoConfiguration() + nested.putAll(newSubPojoGenerator(new PojoConfiguration() .setPackageName(configuration.getPackageName()) .setClassName(className) .setAddJsonbProperty(configuration.isAddJsonbProperty()) - .setAddAllArgsConstructor(configuration.isAddAllArgsConstructor())) + .setAddAllArgsConstructor(configuration.isAddAllArgsConstructor()) + .setOnRef(configuration.getOnRef())) .visitSchema(schema) .generate()); return className; @@ -572,6 +582,16 @@ public class PojoGenerator { private boolean addJsonbProperty = true; private boolean addAllArgsConstructor = true; private boolean fluentSetters = false; + private Function<Ref, String> onRef; + + public Function<Ref, String> getOnRef() { + return onRef; + } + + public PojoConfiguration setOnRef(final Function<Ref, String> onRef) { + this.onRef = onRef; + return this; + } public boolean isFluentSetters() { return fluentSetters; @@ -630,4 +650,35 @@ public class PojoGenerator { this.type = type; } } + + public static class Ref { + private final String ref; + private final Set<String> imports; + private final List<Attribute> attributes; + private final Map<String, String> nested; + + private Ref(final String ref, final Set<String> imports, + final List<Attribute> attributes, final Map<String, String> nested) { + this.ref = ref; + this.imports = imports; + this.attributes = attributes; + this.nested = nested; + } + + public String getRef() { + return ref; + } + + public Set<String> getImports() { + return imports; + } + + public List<Attribute> getAttributes() { + return attributes; + } + + public Map<String, String> getNested() { + return nested; + } + } }