diff --git a/src/main/java/com/ethlo/jsons2xsd/Config.java b/src/main/java/com/ethlo/jsons2xsd/Config.java index 38a68e2..bb504cd 100644 --- a/src/main/java/com/ethlo/jsons2xsd/Config.java +++ b/src/main/java/com/ethlo/jsons2xsd/Config.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.function.Function; public class Config { @@ -40,6 +41,8 @@ public class Config private final boolean validateXsdSchema; private final Map typeMapping; private final boolean ignoreUnknownFormats; + private final String rootElement; + private final Function itemNameMapper; public boolean isAttributesQualified() { @@ -68,7 +71,7 @@ public String getNsAlias() public boolean isCreateRootElement() { - return createRootElement; + return createRootElement || rootElement != null; } public boolean isValidateXsdSchema() @@ -86,6 +89,14 @@ public boolean isIgnoreUnknownFormats() return ignoreUnknownFormats; } + public String getRootElement() { + return rootElement != null ? rootElement : name; + } + + public Function getItemNameMapper() { + return itemNameMapper; + } + public String getType(String type, String format) { final String key = (type + (format != null ? ("|" + format) : "")).toLowerCase(); @@ -103,6 +114,8 @@ public static class Builder private boolean validateXsdSchema = true; private final Map typeMapping = new HashMap<>(); private boolean ignoreUnknownFormats; + private String rootElement; + private Function itemNameMapper = Function.identity(); public Builder targetNamespace(String targetNamespace) { @@ -176,6 +189,16 @@ public Builder ignoreUnknownFormats(final boolean b) this.ignoreUnknownFormats = b; return this; } + + public Builder rootElement(final String name) { + this.rootElement = name; + return this; + } + + public Builder mapArrayItemNames(final Function mapper) { + this.itemNameMapper = mapper; + return this; + } } private Config(Builder builder) @@ -189,5 +212,7 @@ private Config(Builder builder) this.validateXsdSchema = builder.validateXsdSchema; this.typeMapping = builder.typeMapping; this.ignoreUnknownFormats = builder.ignoreUnknownFormats; + this.rootElement = builder.rootElement; + this.itemNameMapper = builder.itemNameMapper; } } diff --git a/src/main/java/com/ethlo/jsons2xsd/Jsons2Xsd.java b/src/main/java/com/ethlo/jsons2xsd/Jsons2Xsd.java index d6c7906..384fa9f 100644 --- a/src/main/java/com/ethlo/jsons2xsd/Jsons2Xsd.java +++ b/src/main/java/com/ethlo/jsons2xsd/Jsons2Xsd.java @@ -175,7 +175,7 @@ private static Element createRootElementIfNeeded(Config cfg, Element schemaRoot) if (cfg.isCreateRootElement()) { final Element wrapper = element(schemaRoot, XSD_ELEMENT); - wrapper.setAttribute(FIELD_NAME, cfg.getName()); + wrapper.setAttribute(FIELD_NAME, cfg.getRootElement()); wrapper.setAttribute("type", cfg.getNsAlias() + ":" + cfg.getName()); } @@ -388,7 +388,7 @@ private static void handleReference(Set neededElements, Element nodeElem if (oldName.length() <= 0) { - nodeElem.setAttribute(FIELD_NAME, name); + nodeElem.setAttribute(FIELD_NAME, cfg.getItemNameMapper().apply(name)); } nodeElem.setAttribute("type", fixRef); diff --git a/src/test/java/com/ethlo/jsons2xsd/ConversionTest.java b/src/test/java/com/ethlo/jsons2xsd/ConversionTest.java index 9832c8e..bde6b46 100644 --- a/src/test/java/com/ethlo/jsons2xsd/ConversionTest.java +++ b/src/test/java/com/ethlo/jsons2xsd/ConversionTest.java @@ -213,6 +213,24 @@ public void testSimpleTypeRefs() throws IOException, TransformerException } } + @Test + public void testCustomNames() throws IOException, TransformerException + { + try (final Reader r = reader("/schema/custom-names.json")) + { + final Config cfg = new Config.Builder() + .createRootElement(true) + .targetNamespace("http://ethlo.com/schema/group-test-1.0.xsd") + .name("Group") + .rootElement("group") + .mapArrayItemNames( name -> name.toUpperCase() ) + .validateXsdSchema(true) + .build(); + final Document doc = Jsons2Xsd.convert(r, cfg); + assertThat(XmlUtil.asXmlString(doc.getDocumentElement())).isXmlEqualTo(load("schema/custom-names.xsd")); + } + } + private Reader reader(String path) { return new InputStreamReader(getClass().getResourceAsStream(path)); diff --git a/src/test/resources/schema/custom-names.json b/src/test/resources/schema/custom-names.json new file mode 100644 index 0000000..ab3acc9 --- /dev/null +++ b/src/test/resources/schema/custom-names.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "name": { "type": "string" }, + "permissions": { + "type": "array", + "items": { + "type": "string" + } + }, + "members": { + "type": "array", + "items": { + "$ref": "#/definitions/Person" + } + } + }, + "definitions": { + "Person": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/schema/custom-names.xsd b/src/test/resources/schema/custom-names.xsd new file mode 100644 index 0000000..a129bc7 --- /dev/null +++ b/src/test/resources/schema/custom-names.xsd @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +