From 7e8ed7b6a37a95a6a62750a749c631087547a884 Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Tue, 21 Aug 2018 21:49:18 -0400 Subject: [PATCH] fixes #165 OverwriteHandler flag needs to be at individual handler --- .../java/com/networknt/codegen/Generator.java | 13 + .../hybrid/HybridServiceGenerator.java | 12 +- .../codegen/rest/OpenApiGenerator.java | 304 +++++++++--------- .../codegen/rest/SwaggerGenerator.java | 211 ++++++------ light-rest-4j/src/test/resources/config.json | 4 +- 5 files changed, 283 insertions(+), 261 deletions(-) diff --git a/codegen-core/src/main/java/com/networknt/codegen/Generator.java b/codegen-core/src/main/java/com/networknt/codegen/Generator.java index 123244ee2..74ba59a6b 100644 --- a/codegen-core/src/main/java/com/networknt/codegen/Generator.java +++ b/codegen-core/src/main/java/com/networknt/codegen/Generator.java @@ -85,4 +85,17 @@ default void transfer(String folder, String path, String filename, DefaultRocker } } + /** + * This is a default method to check if a handler or handler test exists or not before making overwrite decision. + * + * @param folder The output folder of the project + * @param path Current file path in the output folder + * @param filename Current filename in the output folder + * @throws IOException throws IOException + */ + default boolean checkExist(String folder, String path, String filename) throws IOException { + String absPath = folder + (path.isEmpty()? "" : separator + path) + separator + filename; + return Files.exists(Paths.get(absPath)); + } + } diff --git a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java index 790b23989..fb2592879 100644 --- a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java +++ b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java @@ -82,7 +82,10 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep for(Any item : items) { Any any = item.get("example"); String example = any.valueType() != ValueType.INVALID ? StringEscapeUtils.escapeJson(any.toString()).trim() : ""; - if(overwriteHandler) transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), item.get("handler") + ".java", templates.hybrid.handler.template(handlerPackage, host, service, item, example)); + if(!overwriteHandler && checkExist(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), item.get("handler") + ".java")) { + continue; + } + transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), item.get("handler") + ".java", templates.hybrid.handler.template(handlerPackage, host, service, item, example)); String serviceId = host + "/" + service + "/" + item.get("name") + "/" + item.get("version"); Map map = new HashMap<>(); map.put("schema", item.get("schema")); @@ -92,10 +95,11 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep // handler test cases transfer(targetPath, ("src.test.java." + handlerPackage + ".").replace(".", separator), "TestServer.java", templates.hybrid.testServer.template(handlerPackage)); - if(overwriteHandlerTest) { - for(Any item : items) { - transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), item.get("handler") + "Test.java", templates.hybrid.handlerTest.template(handlerPackage, host, service, item)); + for(Any item : items) { + if(!overwriteHandlerTest && checkExist(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), item.get("handler") + "Test.java")) { + continue; } + transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), item.get("handler") + "Test.java", templates.hybrid.handlerTest.template(handlerPackage, host, service, item)); } } diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index 4b19201e9..3731c66dc 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -137,188 +137,192 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep transfer(targetPath, ("src.main.resources.config").replace(".", separator), "handler.yml", templates.rest.openapi.handlerYml.template(serviceId, handlerPackage, operationList, prometheusMetrics)); // model - if(overwriteModel) { - Any anyComponents; - if(model instanceof Any) { - anyComponents = ((Any)model).get("components"); - } else if(model instanceof String){ - // this must be yaml format and we need to convert to json for JsonIterator. - OpenApi3 openApi3 = null; - try { - openApi3 = (OpenApi3) new OpenApiParser().parse((String)model, new URL("https://oas.lightapi.net/")); - } catch (MalformedURLException e) { - throw new RuntimeException("Failed to parse the model", e); - } - anyComponents = JsonIterator.deserialize(openApi3.toJson().toString()).get("components"); - } else { - throw new RuntimeException("Invalid Model Class: " + model.getClass()); + Any anyComponents; + if(model instanceof Any) { + anyComponents = ((Any)model).get("components"); + } else if(model instanceof String){ + // this must be yaml format and we need to convert to json for JsonIterator. + OpenApi3 openApi3 = null; + try { + openApi3 = (OpenApi3) new OpenApiParser().parse((String)model, new URL("https://oas.lightapi.net/")); + } catch (MalformedURLException e) { + throw new RuntimeException("Failed to parse the model", e); } - if(anyComponents.valueType() != ValueType.INVALID) { - Any schemas = anyComponents.asMap().get("schemas"); - if(schemas != null && schemas.valueType() != ValueType.INVALID) { - for(Map.Entry entry : schemas.asMap().entrySet()) { - List> props = new ArrayList<>(); - String key = entry.getKey(); - Map value = entry.getValue().asMap(); - String type = null; - boolean isEnum = false; - Map properties = null; - List required = null; + anyComponents = JsonIterator.deserialize(openApi3.toJson().toString()).get("components"); + } else { + throw new RuntimeException("Invalid Model Class: " + model.getClass()); + } + if(anyComponents.valueType() != ValueType.INVALID) { + Any schemas = anyComponents.asMap().get("schemas"); + if(schemas != null && schemas.valueType() != ValueType.INVALID) { + for(Map.Entry entry : schemas.asMap().entrySet()) { + List> props = new ArrayList<>(); + String key = entry.getKey(); + Map value = entry.getValue().asMap(); + String type = null; + boolean isEnum = false; + Map properties = null; + List required = null; - for(Map.Entry entrySchema: value.entrySet()) { - if("type".equals(entrySchema.getKey())) { - type = entrySchema.getValue().toString(); - if("enum".equals(type)) isEnum = true; - } - if("properties".equals(entrySchema.getKey())) { - properties = entrySchema.getValue().asMap(); - // transform properties + for(Map.Entry entrySchema: value.entrySet()) { + if("type".equals(entrySchema.getKey())) { + type = entrySchema.getValue().toString(); + if("enum".equals(type)) isEnum = true; + } + if("properties".equals(entrySchema.getKey())) { + properties = entrySchema.getValue().asMap(); + // transform properties - for(Map.Entry entryProp: properties.entrySet()) { - //System.out.println("key = " + entryProp.getKey() + " value = " + entryProp.getValue()); - Map propMap = new HashMap<>(); - String name = entryProp.getKey(); - propMap.put("jsonProperty", Any.wrap(name)); - if(name.startsWith("@")) { - name = name.substring(1); + for(Map.Entry entryProp: properties.entrySet()) { + //System.out.println("key = " + entryProp.getKey() + " value = " + entryProp.getValue()); + Map propMap = new HashMap<>(); + String name = entryProp.getKey(); + propMap.put("jsonProperty", Any.wrap(name)); + if(name.startsWith("@")) { + name = name.substring(1); - } - propMap.put("name", Any.wrap(name)); - propMap.put("getter", Any.wrap("get" + name.substring(0, 1).toUpperCase() + name.substring(1))); - propMap.put("setter", Any.wrap("set" + name.substring(0, 1).toUpperCase() + name.substring(1))); - // assume it is not enum unless it is overwritten - propMap.put("isEnum", Any.wrap(false)); + } + propMap.put("name", Any.wrap(name)); + propMap.put("getter", Any.wrap("get" + name.substring(0, 1).toUpperCase() + name.substring(1))); + propMap.put("setter", Any.wrap("set" + name.substring(0, 1).toUpperCase() + name.substring(1))); + // assume it is not enum unless it is overwritten + propMap.put("isEnum", Any.wrap(false)); - boolean isArray = false; - for(Map.Entry entryElement: entryProp.getValue().asMap().entrySet()) { - //System.out.println("key = " + entryElement.getKey() + " value = " + entryElement.getValue()); + boolean isArray = false; + for(Map.Entry entryElement: entryProp.getValue().asMap().entrySet()) { + //System.out.println("key = " + entryElement.getKey() + " value = " + entryElement.getValue()); - if("type".equals(entryElement.getKey())) { - String t = typeMapping.get(entryElement.getValue().toString()); - if("java.util.List".equals(t)) { - isArray = true; - } else { - propMap.put("type", Any.wrap(t)); - } + if("type".equals(entryElement.getKey())) { + String t = typeMapping.get(entryElement.getValue().toString()); + if("java.util.List".equals(t)) { + isArray = true; + } else { + propMap.put("type", Any.wrap(t)); } - if("items".equals(entryElement.getKey())) { - Any a = entryElement.getValue(); - if(a.get("$ref").valueType() != ValueType.INVALID && isArray) { - String s = a.get("$ref").toString(); - s = s.substring(s.lastIndexOf('/') + 1); - propMap.put("type", Any.wrap("java.util.List<" + s + ">")); - } - if(a.get("type").valueType() != ValueType.INVALID && isArray) { - propMap.put("type", Any.wrap("java.util.List<" + typeMapping.get(a.get("type").toString()) + ">")); - } - } - if("$ref".equals(entryElement.getKey())) { - String s = entryElement.getValue().toString(); + } + if("items".equals(entryElement.getKey())) { + Any a = entryElement.getValue(); + if(a.get("$ref").valueType() != ValueType.INVALID && isArray) { + String s = a.get("$ref").toString(); s = s.substring(s.lastIndexOf('/') + 1); - propMap.put("type", Any.wrap(s)); + propMap.put("type", Any.wrap("java.util.List<" + s + ">")); + } + if(a.get("type").valueType() != ValueType.INVALID && isArray) { + propMap.put("type", Any.wrap("java.util.List<" + typeMapping.get(a.get("type").toString()) + ">")); + } + } + if("$ref".equals(entryElement.getKey())) { + String s = entryElement.getValue().toString(); + s = s.substring(s.lastIndexOf('/') + 1); + propMap.put("type", Any.wrap(s)); + } + if("default".equals(entryElement.getKey())) { + Any a = entryElement.getValue(); + propMap.put("default", a); + } + if("enum".equals(entryElement.getKey())) { + propMap.put("isEnum", Any.wrap(true)); + propMap.put("nameWithEnum", Any.wrap(name.substring(0, 1).toUpperCase() + name.substring(1) + "Enum")); + propMap.put("value", Any.wrap(entryElement.getValue())); + } + if("format".equals(entryElement.getKey())) { + String s = entryElement.getValue().toString(); + if("date-time".equals(s)) { + propMap.put("type", Any.wrap("java.time.LocalDateTime")); + } + if("date".equals(s)) { + propMap.put("type", Any.wrap("java.time.LocalDate")); } - if("default".equals(entryElement.getKey())) { - Any a = entryElement.getValue(); - propMap.put("default", a); + if("double".equals(s)) { + propMap.put("type", Any.wrap(s)); } - if("enum".equals(entryElement.getKey())) { - propMap.put("isEnum", Any.wrap(true)); - propMap.put("nameWithEnum", Any.wrap(name.substring(0, 1).toUpperCase() + name.substring(1) + "Enum")); - propMap.put("value", Any.wrap(entryElement.getValue())); + if("float".equals(s)) { + propMap.put("type", Any.wrap(s)); } - if("format".equals(entryElement.getKey())) { - String s = entryElement.getValue().toString(); - if("date-time".equals(s)) { - propMap.put("type", Any.wrap("java.time.LocalDateTime")); - } - if("date".equals(s)) { - propMap.put("type", Any.wrap("java.time.LocalDate")); - } - if("double".equals(s)) { - propMap.put("type", Any.wrap(s)); - } - if("float".equals(s)) { - propMap.put("type", Any.wrap(s)); - } - if("int64".equals(s)){ - propMap.put("type", Any.wrap("java.lang.Long")); - } + if("int64".equals(s)){ + propMap.put("type", Any.wrap("java.lang.Long")); } - if("oneOf".equals(entryElement.getKey())) { - List list = entryElement.getValue().asList(); - String t = list.get(0).asMap().get("type").toString(); - if(t != null) { - propMap.put("type", Any.wrap(typeMapping.get(t))); - } else { - // maybe reference? default type to object. - propMap.put("type", Any.wrap("Object")); - } + } + if("oneOf".equals(entryElement.getKey())) { + List list = entryElement.getValue().asList(); + String t = list.get(0).asMap().get("type").toString(); + if(t != null) { + propMap.put("type", Any.wrap(typeMapping.get(t))); + } else { + // maybe reference? default type to object. + propMap.put("type", Any.wrap("Object")); } - if("anyOf".equals(entryElement.getKey())) { - List list = entryElement.getValue().asList(); - String t = list.get(0).asMap().get("type").toString(); - if(t != null) { - propMap.put("type", Any.wrap(typeMapping.get(t))); - } else { - // maybe reference? default type to object. - propMap.put("type", Any.wrap("Object")); - } + } + if("anyOf".equals(entryElement.getKey())) { + List list = entryElement.getValue().asList(); + String t = list.get(0).asMap().get("type").toString(); + if(t != null) { + propMap.put("type", Any.wrap(typeMapping.get(t))); + } else { + // maybe reference? default type to object. + propMap.put("type", Any.wrap("Object")); } - if("allOf".equals(entryElement.getKey())) { - List list = entryElement.getValue().asList(); - String t = list.get(0).asMap().get("type").toString(); - if(t != null) { - propMap.put("type", Any.wrap(typeMapping.get(t))); - } else { - // maybe reference? default type to object. - propMap.put("type", Any.wrap("Object")); - } + } + if("allOf".equals(entryElement.getKey())) { + List list = entryElement.getValue().asList(); + String t = list.get(0).asMap().get("type").toString(); + if(t != null) { + propMap.put("type", Any.wrap(typeMapping.get(t))); + } else { + // maybe reference? default type to object. + propMap.put("type", Any.wrap("Object")); } - if("not".equals(entryElement.getKey())) { - Map m = entryElement.getValue().asMap(); - Any t = m.get("type"); - if(t != null) { - propMap.put("type", t); - } else { - propMap.put("type", Any.wrap("Object")); - } + } + if("not".equals(entryElement.getKey())) { + Map m = entryElement.getValue().asMap(); + Any t = m.get("type"); + if(t != null) { + propMap.put("type", t); + } else { + propMap.put("type", Any.wrap("Object")); } } - props.add(propMap); } + props.add(propMap); } - if("required".equals(entrySchema.getKey())) { - required = entrySchema.getValue().asList(); - } } - String classVarName = key; - String modelFileName = key.substring(0, 1).toUpperCase() + key.substring(1); - //System.out.println("props = " + Any.wrap(props)); - transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.pojo.template(modelPackage, modelFileName, classVarName, props)); + if("required".equals(entrySchema.getKey())) { + required = entrySchema.getValue().asList(); + } } + String classVarName = key; + String modelFileName = key.substring(0, 1).toUpperCase() + key.substring(1); + //System.out.println("props = " + Any.wrap(props)); + if(!overwriteModel && checkExist(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java")) { + continue; + } + transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.pojo.template(modelPackage, modelFileName, classVarName, props)); } } } // handler - if(overwriteHandler) { - for(Map op : operationList){ - String className = op.get("handlerName").toString(); - String example = null; - if(op.get("example") != null) { - //example = mapper.writeValueAsString(op.get("example")); - example = JsonStream.serialize(op.get("example")); - } - transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example)); + for(Map op : operationList){ + String className = op.get("handlerName").toString(); + String example = null; + if(op.get("example") != null) { + //example = mapper.writeValueAsString(op.get("example")); + example = JsonStream.serialize(op.get("example")); } + if(checkExist(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java") && !overwriteHandler) { + continue; + } + transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example)); } // handler test cases transfer(targetPath, ("src.test.java." + handlerPackage + ".").replace(".", separator), "TestServer.java", templates.rest.testServer.template(handlerPackage)); - if(overwriteHandlerTest) { - for(Map op : operationList){ - transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), op.get("handlerName") + "Test.java", templates.rest.openapi.handlerTest.template(handlerPackage, op)); + + for(Map op : operationList){ + if(checkExist(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), op.get("handlerName") + "Test.java") && !overwriteHandlerTest) { + continue; } + transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), op.get("handlerName") + "Test.java", templates.rest.openapi.handlerTest.template(handlerPackage, op)); } // transfer binary files without touching them. diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java index 006ffa914..b93ed80b6 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java @@ -136,138 +136,139 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep // model - if(overwriteModel) { - Any any = ((Any)model).get("definitions"); - if(any.valueType() != ValueType.INVALID) { - for(Map.Entry entry : any.asMap().entrySet()) { - List> props = new ArrayList<>(); - String key = entry.getKey(); - Map value = entry.getValue().asMap(); - String type = null; - boolean isEnum = false; - Map properties = null; - List required = null; - - for(Map.Entry entrySchema: value.entrySet()) { - if("type".equals(entrySchema.getKey())) { - type = entrySchema.getValue().toString(); - if("enum".equals(type)) isEnum = true; - } - if("properties".equals(entrySchema.getKey())) { - properties = entrySchema.getValue().asMap(); - // transform properties - - for(Map.Entry entryProp: properties.entrySet()) { - //System.out.println("key = " + entryProp.getKey() + " value = " + entryProp.getValue()); - Map propMap = new HashMap<>(); - String name = entryProp.getKey(); - propMap.put("jsonProperty", Any.wrap(name)); - if(name.startsWith("@")) { - name = name.substring(1); + Any any = ((Any)model).get("definitions"); + if(any.valueType() != ValueType.INVALID) { + for(Map.Entry entry : any.asMap().entrySet()) { + List> props = new ArrayList<>(); + String key = entry.getKey(); + Map value = entry.getValue().asMap(); + String type = null; + boolean isEnum = false; + Map properties = null; + List required = null; + + for(Map.Entry entrySchema: value.entrySet()) { + if("type".equals(entrySchema.getKey())) { + type = entrySchema.getValue().toString(); + if("enum".equals(type)) isEnum = true; + } + if("properties".equals(entrySchema.getKey())) { + properties = entrySchema.getValue().asMap(); + // transform properties + + for(Map.Entry entryProp: properties.entrySet()) { + //System.out.println("key = " + entryProp.getKey() + " value = " + entryProp.getValue()); + Map propMap = new HashMap<>(); + String name = entryProp.getKey(); + propMap.put("jsonProperty", Any.wrap(name)); + if(name.startsWith("@")) { + name = name.substring(1); + } + propMap.put("name", Any.wrap(name)); + propMap.put("getter", Any.wrap("get" + name.substring(0, 1).toUpperCase() + name.substring(1))); + propMap.put("setter", Any.wrap("set" + name.substring(0, 1).toUpperCase() + name.substring(1))); + // assume it is not enum unless it is overwritten + propMap.put("isEnum", Any.wrap(false)); + + boolean isArray = false; + for(Map.Entry entryElement: entryProp.getValue().asMap().entrySet()) { + //System.out.println("key = " + entryElement.getKey() + " value = " + entryElement.getValue()); + + if("type".equals(entryElement.getKey())) { + String t = typeMapping.get(entryElement.getValue().toString()); + if("java.util.List".equals(t)) { + isArray = true; + } else { + propMap.put("type", Any.wrap(t)); + } } - propMap.put("name", Any.wrap(name)); - propMap.put("getter", Any.wrap("get" + name.substring(0, 1).toUpperCase() + name.substring(1))); - propMap.put("setter", Any.wrap("set" + name.substring(0, 1).toUpperCase() + name.substring(1))); - // assume it is not enum unless it is overwritten - propMap.put("isEnum", Any.wrap(false)); - - boolean isArray = false; - for(Map.Entry entryElement: entryProp.getValue().asMap().entrySet()) { - //System.out.println("key = " + entryElement.getKey() + " value = " + entryElement.getValue()); - - if("type".equals(entryElement.getKey())) { - String t = typeMapping.get(entryElement.getValue().toString()); - if("java.util.List".equals(t)) { - isArray = true; - } else { - propMap.put("type", Any.wrap(t)); - } + if("items".equals(entryElement.getKey())) { + Any a = entryElement.getValue(); + if(a.get("$ref").valueType() != ValueType.INVALID && isArray) { + String s = a.get("$ref").toString(); + s = s.substring(s.lastIndexOf('/') + 1); + propMap.put("type", Any.wrap("java.util.List<" + s + ">")); } - if("items".equals(entryElement.getKey())) { - Any a = entryElement.getValue(); - if(a.get("$ref").valueType() != ValueType.INVALID && isArray) { - String s = a.get("$ref").toString(); - s = s.substring(s.lastIndexOf('/') + 1); - propMap.put("type", Any.wrap("java.util.List<" + s + ">")); - } - if(a.get("type").valueType() != ValueType.INVALID && isArray) { - propMap.put("type", Any.wrap("java.util.List<" + typeMapping.get(a.get("type").toString()) + ">")); - } + if(a.get("type").valueType() != ValueType.INVALID && isArray) { + propMap.put("type", Any.wrap("java.util.List<" + typeMapping.get(a.get("type").toString()) + ">")); } - if("$ref".equals(entryElement.getKey())) { - String s = entryElement.getValue().toString(); - s = s.substring(s.lastIndexOf('/') + 1); - propMap.put("type", Any.wrap(s)); + } + if("$ref".equals(entryElement.getKey())) { + String s = entryElement.getValue().toString(); + s = s.substring(s.lastIndexOf('/') + 1); + propMap.put("type", Any.wrap(s)); + } + if("default".equals(entryElement.getKey())) { + Any a = entryElement.getValue(); + propMap.put("default", a); + } + if("enum".equals(entryElement.getKey())) { + propMap.put("isEnum", Any.wrap(true)); + propMap.put("nameWithEnum", Any.wrap(name.substring(0, 1).toUpperCase() + name.substring(1) + "Enum")); + propMap.put("value", Any.wrap(entryElement.getValue())); + } + if("format".equals(entryElement.getKey())) { + String s = entryElement.getValue().toString(); + if("date-time".equals(s)) { + propMap.put("type", Any.wrap("java.time.LocalDateTime")); + } + if("date".equals(s)) { + propMap.put("type", Any.wrap("java.time.LocalDate")); } - if("default".equals(entryElement.getKey())) { - Any a = entryElement.getValue(); - propMap.put("default", a); + if("double".equals(s)) { + propMap.put("type", Any.wrap(s)); } - if("enum".equals(entryElement.getKey())) { - propMap.put("isEnum", Any.wrap(true)); - propMap.put("nameWithEnum", Any.wrap(name.substring(0, 1).toUpperCase() + name.substring(1) + "Enum")); - propMap.put("value", Any.wrap(entryElement.getValue())); + if("float".equals(s)) { + propMap.put("type", Any.wrap(s)); } - if("format".equals(entryElement.getKey())) { - String s = entryElement.getValue().toString(); - if("date-time".equals(s)) { - propMap.put("type", Any.wrap("java.time.LocalDateTime")); - } - if("date".equals(s)) { - propMap.put("type", Any.wrap("java.time.LocalDate")); - } - if("double".equals(s)) { - propMap.put("type", Any.wrap(s)); - } - if("float".equals(s)) { - propMap.put("type", Any.wrap(s)); - } - if("int64".equals(s)){ - propMap.put("type", Any.wrap("java.lang.Long")); - } + if("int64".equals(s)){ + propMap.put("type", Any.wrap("java.lang.Long")); } } - props.add(propMap); } - } - if("required".equals(entrySchema.getKey())) { - required = entrySchema.getValue().asList(); + props.add(propMap); } } - String classVarName = key; - String modelFileName = key.substring(0, 1).toUpperCase() + key.substring(1); - //System.out.println("props = " + Any.wrap(props)); - transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.pojo.template(modelPackage, modelFileName, classVarName, props)); + if("required".equals(entrySchema.getKey())) { + required = entrySchema.getValue().asList(); + } + } + String classVarName = key; + String modelFileName = key.substring(0, 1).toUpperCase() + key.substring(1); + //System.out.println("props = " + Any.wrap(props)); + if(!overwriteModel && checkExist(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java")) { + continue; } + transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.pojo.template(modelPackage, modelFileName, classVarName, props)); } } - - // TODO implement model generation based on this object. //List> modelList = getPojoList(model); // handler - if(overwriteHandler) { - for(Map op : operationList){ - String className = op.get("handlerName").toString(); - String example = null; - if(op.get("example") != null) { - //example = mapper.writeValueAsString(op.get("example")); - example = JsonStream.serialize(op.get("example")); - } - transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example)); + for(Map op : operationList){ + String className = op.get("handlerName").toString(); + String example = null; + if(op.get("example") != null) { + //example = mapper.writeValueAsString(op.get("example")); + example = JsonStream.serialize(op.get("example")); + } + if(checkExist(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java") && !overwriteHandler) { + continue; } + transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example)); } // handler test cases transfer(targetPath, ("src.test.java." + handlerPackage + ".").replace(".", separator), "TestServer.java", templates.rest.testServer.template(handlerPackage)); - if(overwriteHandlerTest) { - for(Map op : operationList){ - transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), op.get("handlerName") + "Test.java", templates.rest.swagger.handlerTest.template(handlerPackage, op)); + for(Map op : operationList){ + if(checkExist(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), op.get("handlerName") + "Test.java") && !overwriteHandlerTest) { + continue; } + transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), op.get("handlerName") + "Test.java", templates.rest.swagger.handlerTest.template(handlerPackage, op)); } // transfer binary files without touching them. diff --git a/light-rest-4j/src/test/resources/config.json b/light-rest-4j/src/test/resources/config.json index 89e742956..0d34ea488 100644 --- a/light-rest-4j/src/test/resources/config.json +++ b/light-rest-4j/src/test/resources/config.json @@ -6,8 +6,8 @@ "rootPackage": "com.networknt.petstore", "handlerPackage":"com.networknt.petstore.handler", "modelPackage":"com.networknt.petstore.model", - "overwriteHandler": true, - "overwriteHandlerTest": true, + "overwriteHandler": false, + "overwriteHandlerTest": false, "overwriteModel": true, "httpPort": 8080, "enableHttp": true,