diff --git a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/common/tools/ApiTool.java b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/common/tools/ApiTool.java index 4ba885fc..baeb8090 100644 --- a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/common/tools/ApiTool.java +++ b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/common/tools/ApiTool.java @@ -1,17 +1,32 @@ package com.sngular.api.generator.plugin.common.tools; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; + import com.fasterxml.jackson.databind.JsonNode; import com.sngular.api.generator.plugin.openapi.model.TypeConstants; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.IteratorUtils; import org.apache.commons.collections4.Transformer; -import org.codehaus.plexus.util.StringUtils; - -import java.util.*; -import java.util.Map.Entry; +import org.apache.commons.lang3.StringUtils; public final class ApiTool { + public static final String FORMAT = "format"; + public static final String ALL_OF = "allOf"; + public static final String ANY_OF = "anyOf"; + public static final String ONE_OF = "oneOf"; + public static final String COMPONENTS = "components"; + public static final String SCHEMAS = "schemas"; + public static final String REQUIRED = "required"; + private ApiTool() { } @@ -32,19 +47,19 @@ public static JsonNode getAdditionalProperties(final JsonNode schema) { } public static String getFormat(final JsonNode schema) { - return getNodeAsString(schema, "format"); + return getNodeAsString(schema, FORMAT); } public static JsonNode getAllOf(final JsonNode schema) { - return getNode(schema, "allOf"); + return getNode(schema, ALL_OF); } public static JsonNode getAnyOf(final JsonNode schema) { - return getNode(schema, "anyOf"); + return getNode(schema, ANY_OF); } public static JsonNode getOneOf(final JsonNode schema) { - return getNode(schema, "oneOf"); + return getNode(schema, ONE_OF); } public static JsonNode getNode(final JsonNode schema, final String nodeName) { @@ -80,10 +95,10 @@ public static JsonNode getItems(final JsonNode schema) { public static Map getComponentSchemas(final JsonNode openApi) { final var schemasMap = new HashMap(); - if (hasNode(openApi, "components")) { - final var components = getNode(openApi, "components"); - if (hasNode(components, "schemas")) { - final var schemas = getNode(components, "schemas"); + if (hasNode(openApi, COMPONENTS)) { + final var components = getNode(openApi, COMPONENTS); + if (hasNode(components, SCHEMAS)) { + final var schemas = getNode(components, SCHEMAS); final var schemasIt = schemas.fieldNames(); schemasIt.forEachRemaining(name -> schemasMap.put(name, getNode(schemas, name))); } @@ -95,8 +110,8 @@ public static Map getComponentSchemas(final JsonNode openApi) public static Map getComponentSecuritySchemes(final JsonNode openApi) { final var schemasMap = new HashMap(); - if (hasNode(openApi, "components")) { - final var components = getNode(openApi, "components"); + if (hasNode(openApi, COMPONENTS)) { + final var components = getNode(openApi, COMPONENTS); if (hasNode(components, "securitySchemes")) { getNode(components, "securitySchemes").fields().forEachRemaining(schema -> schemasMap.put(schema.getKey(), schema.getValue())); } @@ -142,7 +157,7 @@ public static boolean hasField(final JsonNode schema, final String... fieldNameA } public static boolean hasRequired(final JsonNode schema) { - return hasNode(schema, "required"); + return hasNode(schema, REQUIRED); } public static boolean hasType(final JsonNode schema) { @@ -174,7 +189,7 @@ public static boolean isArray(final JsonNode schema) { } public static boolean isComposed(final JsonNode schema) { - return ApiTool.hasField(schema, "anyOf", "allOf", "oneOf"); + return ApiTool.hasField(schema, ANY_OF, ALL_OF, ONE_OF); } public static boolean isString(final JsonNode schema) { @@ -198,23 +213,23 @@ public static boolean isEnum(final JsonNode schema) { } public static boolean isAllOf(final JsonNode schema) { - return hasNode(schema, "allOf"); + return hasNode(schema, ALL_OF); } public static boolean isAnyOf(final JsonNode schema) { - return hasNode(schema, "anyOf"); + return hasNode(schema, ANY_OF); } public static boolean isOneOf(final JsonNode schema) { - return hasNode(schema, "oneOf"); + return hasNode(schema, ONE_OF); } public static boolean isDateTime(final JsonNode schema) { final boolean isDateTime; if (hasType(schema) && TypeConstants.STRING.equalsIgnoreCase(getType(schema))) { - if (hasNode(schema, "format")) { - isDateTime = "date".equalsIgnoreCase(getNode(schema, "format").textValue()) - || "date-time".equalsIgnoreCase(getNode(schema, "format").textValue()); + if (hasNode(schema, FORMAT)) { + isDateTime = "date".equalsIgnoreCase(getNode(schema, FORMAT).textValue()) + || "date-time".equalsIgnoreCase(getNode(schema, FORMAT).textValue()); } else { isDateTime = false; } @@ -230,8 +245,8 @@ public static List findContentSchemas(final JsonNode schema) { public static boolean checkIfRequired(final JsonNode schema, final String fieldName) { boolean isRequired = false; - if (hasNode(schema, "required")) { - final var fieldIt = getNode(schema, "required").elements(); + if (hasNode(schema, REQUIRED)) { + final var fieldIt = getNode(schema, REQUIRED).elements(); while (fieldIt.hasNext() && !isRequired) { isRequired = fieldName.equalsIgnoreCase(fieldIt.next().textValue()); } diff --git a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/OpenApiGenerator.java b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/OpenApiGenerator.java index 5b308f31..a92e1d28 100644 --- a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/OpenApiGenerator.java +++ b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/OpenApiGenerator.java @@ -68,7 +68,7 @@ public class OpenApiGenerator { private final File targetFolder; - private final File baseDir; + private final Path baseDir; private Boolean isWebClient = false; @@ -89,7 +89,7 @@ public OpenApiGenerator( this.processedGeneratedSourcesFolder = processedGeneratedSourcesFolder; this.groupId = groupId; this.targetFolder = targetFolder; - this.baseDir = basedir; + this.baseDir = basedir.toPath().toAbsolutePath(); this.targetFileFilter = (dir, name) -> name.toLowerCase().contains(targetFolder.toPath().getFileName().toString()); this.springBootVersion = springBootVersion; } @@ -111,7 +111,7 @@ public final void processFileSpec(final List specsListFile) { private void processFile(final SpecFile specFile, final String filePathToSave) throws IOException { - final JsonNode openAPI = OpenApiUtil.getPojoFromSpecFile(specFile); + final JsonNode openAPI = OpenApiUtil.getPojoFromSpecFile(baseDir, specFile); final String clientPackage = specFile.getClientPackage(); if (specFile.isCallMode()) { @@ -169,7 +169,7 @@ private GlobalObject createApiTemplate(final SpecFile specFile, final String fil for (Map.Entry> apisEntry : apis.entrySet()) { final String javaFileName = OpenApiUtil.processJavaFileName(apisEntry.getKey()); - final List pathObjects = MapperPathUtil.mapPathObjects(openAPI, specFile, apisEntry, globalObject); + final List pathObjects = MapperPathUtil.mapPathObjects(openAPI, specFile, apisEntry, globalObject, baseDir); final AuthObject authObject = MapperAuthUtil.getApiAuthObject(globalObject.getAuthSchemas(), pathObjects); try { @@ -227,12 +227,12 @@ private String processModelPackage(final String modelPackage) { private String processPath(final String fileSpecPackage, final boolean isModel) throws IOException { Path path; - final File[] pathList = Objects.requireNonNull(baseDir.listFiles(targetFileFilter)); + final File[] pathList = Objects.requireNonNull(baseDir.toFile().listFiles(targetFileFilter)); if (pathList.length > 0) { path = pathList[0].toPath().resolve(convertPackageToTargetPath(fileSpecPackage, isModel)); } else { path = targetFolder.toPath(); - if (path.toFile().mkdirs()) { + if (path.toFile().exists() || path.toFile().mkdirs()) { path = path.resolve(convertPackageToTargetPath(fileSpecPackage, isModel)); } else { throw new IOException("Problem creating folders: " + path.toFile()); diff --git a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/MapperPathUtil.java b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/MapperPathUtil.java index 8b9af16b..06f87834 100644 --- a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/MapperPathUtil.java +++ b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/MapperPathUtil.java @@ -6,6 +6,7 @@ package com.sngular.api.generator.plugin.openapi.utils; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -31,8 +32,8 @@ import com.sngular.api.generator.plugin.openapi.model.SchemaFieldObjectType; import com.sngular.api.generator.plugin.openapi.model.TypeConstants; import com.sngular.api.generator.plugin.openapi.parameter.SpecFile; +import org.apache.commons.collections4.IteratorUtils; import org.apache.commons.lang3.StringUtils; -import org.codehaus.plexus.util.CollectionUtils; public class MapperPathUtil { @@ -65,13 +66,13 @@ private static List getSecurityRequirementList(final JsonNode securityNo } public static List mapPathObjects( - final JsonNode openAPI, final SpecFile specFile, final Entry> path, final GlobalObject globalObject) { + final JsonNode openAPI, final SpecFile specFile, final Entry> path, final GlobalObject globalObject, final Path baseDir) { final List pathObjects = new ArrayList<>(); for (Entry pathItem : path.getValue().entrySet()) { final PathObject pathObject = PathObject.builder() .pathName(pathItem.getKey()) .globalObject(globalObject) - .operationObjects(mapOperationObject(openAPI, specFile, pathItem, globalObject)) + .operationObjects(mapOperationObject(openAPI, specFile, pathItem, globalObject, baseDir)) .build(); pathObjects.add(pathObject); } @@ -79,7 +80,7 @@ public static List mapPathObjects( return pathObjects; } - private static List mapOperationObject(final JsonNode openAPI, final SpecFile specFile, final Entry path, final GlobalObject globalObject) { + private static List mapOperationObject(final JsonNode openAPI, final SpecFile specFile, final Entry path, final GlobalObject globalObject, final Path baseDir) { final List operationObjects = new ArrayList<>(); final List operationIdList = new ArrayList<>(); final var pathNode = path.getValue(); @@ -92,10 +93,10 @@ private static List mapOperationObject(final JsonNode openAPI, case "delete": case "put": case "patch": - operationObjects.add(createOperation(openAPI, field.getValue(), field.getKey().toUpperCase(), specFile, globalObject, operationIdList)); + operationObjects.add(createOperation(openAPI, field.getValue(), field.getKey().toUpperCase(), specFile, globalObject, operationIdList, baseDir)); break; case "parameters": - pathParameters.addAll(mapParameterObjects(openAPI, CollectionUtils.iteratorToList(field.getValue().elements()), specFile, null, globalObject)); + pathParameters.addAll(mapParameterObjects(openAPI, IteratorUtils.toList(field.getValue().elements()), specFile, null, globalObject, baseDir)); break; default: break; @@ -112,17 +113,17 @@ private static List mapOperationObject(final JsonNode openAPI, } private static OperationObject createOperation( - final JsonNode openAPI, final JsonNode operation, final String operationType, - final SpecFile specFile, final GlobalObject globalObject, final List operationIdList) { + final JsonNode openAPI, final JsonNode operation, final String operationType, + final SpecFile specFile, final GlobalObject globalObject, final List operationIdList, final Path baseDir) { return OperationObject.builder() .operationId(mapOperationId(getOperationId(operation), operationIdList)) .operationType(operationType) .summary(ApiTool.getNodeAsString(operation, "summary")) .tags(elementsToStrList(operation.get("tags").elements())) - .requestObjects(mapRequestObject(specFile, operation, globalObject)) - .responseObjects(mapResponseObject(specFile, operation, globalObject)) - .parameterObjects(mapParameterObjects(openAPI, CollectionUtils.iteratorToList(operation.at("/parameters").elements()), specFile, - getOperationId(operation), globalObject)) + .requestObjects(mapRequestObject(specFile, operation, globalObject, baseDir)) + .responseObjects(mapResponseObject(specFile, operation, globalObject, baseDir)) + .parameterObjects(mapParameterObjects(openAPI, IteratorUtils.toList(operation.at("/parameters").elements()), specFile, + getOperationId(operation), globalObject, baseDir)) .securities(getSecurityRequirementList(operation.path("/security"), globalObject.getAuthentications())) .consumes(getRequestList(operation.at("/requestBody"))) .produces(getResponseList(operation.at("/responses"))) @@ -182,7 +183,8 @@ private static List getResponseList(final JsonNode responses) { return producesList; } - private static List mapRequestObject(final SpecFile specFile, final JsonNode operation, final GlobalObject globalObject) { + private static List mapRequestObject(final SpecFile specFile, final JsonNode operation, + final GlobalObject globalObject, final Path baseDir) { final List requestObjects = new ArrayList<>(); if (Objects.isNull(getOperationId(operation))) { throw new InvalidOpenAPIException(); @@ -193,39 +195,40 @@ private static List mapRequestObject(final SpecFile specFile, fin final var requestBody = ApiTool.getNode(operation, REQUEST_BODY); requestObjects.add(RequestObject.builder() .required(ApiTool.hasNode(requestBody, REQUIRED)) - .contentObjects(mapContentObject(specFile, ApiTool.getNode(requestBody, CONTENT), "InlineObject" + operationIdWithCap, globalObject)) + .contentObjects(mapContentObject(specFile, ApiTool.getNode(requestBody, CONTENT), + "InlineObject" + operationIdWithCap, globalObject, baseDir)) .build()); } return requestObjects; } private static List mapParameterObjects( - final JsonNode openAPI, final List parameters, final SpecFile specFile, final String contentClassName, - final GlobalObject globalObject) { + final JsonNode openAPI, final List parameters, final SpecFile specFile, final String contentClassName, + final GlobalObject globalObject, Path baseDir) { final List parameterObjects = new ArrayList<>(); if (Objects.nonNull(parameters) && !parameters.isEmpty()) { for (JsonNode parameter : parameters) { if (ApiTool.hasRef(parameter)) { final String refName = MapperUtil.getRefSchemaName(parameter); final JsonNode refParameter = openAPI.path(refName); - parameterObjects.add(buildParameterObject(specFile, globalObject, refParameter)); + parameterObjects.add(buildParameterObject(specFile, globalObject, refParameter, baseDir)); } else if (ApiTool.hasNode(parameter, CONTENT)) { - parameterObjects.addAll(buildParameterContent(contentClassName, parameter, specFile, globalObject)); + parameterObjects.addAll(buildParameterContent(contentClassName, parameter, specFile, globalObject, baseDir)); } else { - parameterObjects.add(buildParameterObject(specFile, globalObject, parameter)); + parameterObjects.add(buildParameterObject(specFile, globalObject, parameter, baseDir)); } } } return parameterObjects; } - private static ParameterObject buildParameterObject(final SpecFile specFile, final GlobalObject globalObject, final JsonNode refParameter) { + private static ParameterObject buildParameterObject(final SpecFile specFile, final GlobalObject globalObject, final JsonNode refParameter, Path baseDir) { return ParameterObject.builder() .name(ApiTool.getName(refParameter)) .required(ApiTool.getNodeAsBoolean(refParameter, REQUIRED)) .description(ApiTool.getNodeAsString(refParameter, DESCRIPTION)) .in(ApiTool.getNodeAsString(refParameter, "in")) - .dataType(getSchemaType(getContentOrSchema(refParameter), TypeConstants.OBJECT, specFile, globalObject)) + .dataType(getSchemaType(getContentOrSchema(refParameter), TypeConstants.OBJECT, specFile, globalObject, baseDir)) .isCollection(ApiTool.hasItems(getContentOrSchema(refParameter))) .build(); } @@ -241,8 +244,8 @@ private static JsonNode getContentOrSchema(final JsonNode refParameter) { } private static List buildParameterContent( - final String contentClassName, final JsonNode parameter, - final SpecFile specFile, final GlobalObject globalObject) { + final String contentClassName, final JsonNode parameter, final SpecFile specFile, + final GlobalObject globalObject, final Path baseDir) { final var content = ApiTool.getNode(parameter, CONTENT); final var parameterName = ApiTool.getName(parameter); final var parameterObjects = new ArrayList(); @@ -268,7 +271,7 @@ private static List buildParameterContent( } else { parameterObjects.add(builder .name(parameterName) - .dataType(getSchemaType(parameterSchema, inlineParameterPojo, specFile, globalObject)) + .dataType(getSchemaType(parameterSchema, inlineParameterPojo, specFile, globalObject, baseDir)) .build()); } } @@ -280,7 +283,7 @@ private static String safeCapitalize(final String text) { } private static List mapResponseObject(final SpecFile specFile, final JsonNode operation, - final GlobalObject globalObject) { + final GlobalObject globalObject, final Path baseDir) { final List responseObjects = new ArrayList<>(); if (ApiTool.hasNode(operation, "responses")) { final JsonNode responses = ApiTool.getNode(operation, "responses"); @@ -288,13 +291,14 @@ private static List mapResponseObject(final SpecFile specFile, f responses .fieldNames() .forEachRemaining(responseCode -> - createResponseObject(specFile, globalObject, responseObjects, operationId).accept(responseCode, ApiTool.getNode(responses, responseCode))); + createResponseObject(specFile, globalObject, responseObjects, operationId, baseDir) + .accept(responseCode, ApiTool.getNode(responses, responseCode))); } return responseObjects; } private static BiConsumer createResponseObject(final SpecFile specFile, final GlobalObject globalObject, - final List responseObjects, final String operationId) { + final List responseObjects, final String operationId, final Path baseDir) { return (responseCode, response) -> { final String operationIdWithCap = operationId.substring(0, 1).toUpperCase() + operationId.substring(1); responseObjects.add(ResponseObject @@ -302,19 +306,21 @@ private static BiConsumer createResponseObject(final SpecFile .responseName(responseCode) .description(StringUtils.defaultIfEmpty(ApiTool.getNodeAsString(response, DESCRIPTION), "")) .contentObjects(mapContentObject(specFile, ApiTool.getNode(response, CONTENT), - "InlineResponse" + responseCode + operationIdWithCap, globalObject)) + "InlineResponse" + responseCode + operationIdWithCap, globalObject, baseDir)) .build()); }; } - private static List mapContentObject(final SpecFile specFile, final JsonNode content, final String inlineObject, final GlobalObject globalObject) { + private static List mapContentObject( + final SpecFile specFile, final JsonNode content, final String inlineObject, final GlobalObject globalObject, + final Path baseDir) { final List contentObjects = new ArrayList<>(); if (Objects.nonNull(content)) { for (Iterator it = content.fieldNames(); it.hasNext();) { final String mediaType = it.next(); final var schema = ApiTool.getNode(ApiTool.getNode(content, mediaType), SCHEMA); final String pojoName = preparePojoName(inlineObject, schema, specFile); - final SchemaFieldObjectType dataType = getSchemaType(schema, pojoName, specFile, globalObject); + final SchemaFieldObjectType dataType = getSchemaType(schema, pojoName, specFile, globalObject, baseDir); final String importName = getImportFromType(dataType); contentObjects.add(ContentObject.builder() .dataType(dataType) @@ -343,19 +349,22 @@ private static String preparePojoName(final String inlineObject, final JsonNode return pojoName; } - private static SchemaFieldObjectType getSchemaType(final JsonNode schema, final String pojoName, final SpecFile specFile, final GlobalObject globalObject) { + private static SchemaFieldObjectType getSchemaType( + final JsonNode schema, final String pojoName, final SpecFile specFile, final GlobalObject globalObject, + final Path baseDir) { SchemaFieldObjectType type = null; if (ApiTool.hasRef(schema)) { final String refSchemaPojoName = MapperContentUtil.getRef(schema, specFile); - final JsonNode refSchema = OpenApiUtil.solveRef(ApiTool.getRefValue(schema), globalObject.getSchemaMap(), specFile.getFilePath()); - type = getSchemaType(refSchema, refSchemaPojoName, specFile, globalObject); + final JsonNode refSchema = OpenApiUtil.solveRef(ApiTool.getRefValue(schema), globalObject.getSchemaMap(), + baseDir.resolve(specFile.getFilePath()).getParent()); + type = getSchemaType(refSchema, refSchemaPojoName, specFile, globalObject, baseDir); } else if (ApiTool.hasAdditionalProperties(schema)) { - type = getMapSchemaType(schema, pojoName, specFile, globalObject); + type = getMapSchemaType(schema, pojoName, specFile, globalObject, baseDir); } else if (ApiTool.isDateTime(schema)) { type = new SchemaFieldObjectType(MapperContentUtil.getDateType(schema, specFile)); } else if (ApiTool.hasType(schema)) { - type = getObjectOrType(schema, pojoName, specFile, globalObject); + type = getObjectOrType(schema, pojoName, specFile, globalObject, baseDir); } else if (ApiTool.isComposed(schema)) { type = SchemaFieldObjectType.fromTypeList(TypeConstants.OBJECT, pojoName); } @@ -363,7 +372,9 @@ private static SchemaFieldObjectType getSchemaType(final JsonNode schema, final return type; } - private static SchemaFieldObjectType getObjectOrType(final JsonNode schema, final String pojoName, final SpecFile specFile, final GlobalObject globalObject) { + private static SchemaFieldObjectType getObjectOrType( + final JsonNode schema, final String pojoName, final SpecFile specFile, final GlobalObject globalObject, + final Path baseDir) { final SchemaFieldObjectType type; switch (ApiTool.getType(schema)) { case TypeConstants.OBJECT: @@ -379,7 +390,7 @@ private static SchemaFieldObjectType getObjectOrType(final JsonNode schema, fina type = new SchemaFieldObjectType(TypeConstants.BOOLEAN); break; case TypeConstants.ARRAY: - type = new SchemaFieldObjectType(TypeConstants.ARRAY, getSchemaType(ApiTool.getItems(schema), pojoName, specFile, globalObject)); + type = new SchemaFieldObjectType(TypeConstants.ARRAY, getSchemaType(ApiTool.getItems(schema), pojoName, specFile, globalObject, baseDir)); break; default: type = new SchemaFieldObjectType(TypeConstants.STRING); @@ -387,7 +398,9 @@ private static SchemaFieldObjectType getObjectOrType(final JsonNode schema, fina return type; } - private static SchemaFieldObjectType getMapSchemaType(final JsonNode schema, final String pojoName, final SpecFile specFile, final GlobalObject globalObject) { + private static SchemaFieldObjectType getMapSchemaType( + final JsonNode schema, final String pojoName, final SpecFile specFile, final GlobalObject globalObject, + final Path baseDir) { final SchemaFieldObjectType type; final JsonNode addPropObj = ApiTool.getAdditionalProperties(schema); @@ -396,7 +409,7 @@ private static SchemaFieldObjectType getMapSchemaType(final JsonNode schema, fin } else if (TypeConstants.BOOLEAN.equalsIgnoreCase(ApiTool.getType(addPropObj))) { type = SchemaFieldObjectType.fromTypeList(TypeConstants.MAP, TypeConstants.OBJECT); } else { - type = new SchemaFieldObjectType(TypeConstants.MAP, getSchemaType(addPropObj, pojoName, specFile, globalObject)); + type = new SchemaFieldObjectType(TypeConstants.MAP, getSchemaType(addPropObj, pojoName, specFile, globalObject, baseDir)); } return type; diff --git a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/OpenApiUtil.java b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/OpenApiUtil.java index befd9c0a..f11cd546 100644 --- a/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/OpenApiUtil.java +++ b/multiapi-engine/src/main/java/com/sngular/api/generator/plugin/openapi/utils/OpenApiUtil.java @@ -6,6 +6,22 @@ package com.sngular.api.generator.plugin.openapi.utils; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; + import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.JsonNodeFactory; @@ -19,16 +35,6 @@ import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.apache.commons.lang3.StringUtils; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Paths; -import java.util.*; -import java.util.Map.Entry; - public class OpenApiUtil { static final ObjectMapper PARSER = new ObjectMapper(new YAMLFactory()); @@ -38,7 +44,7 @@ public class OpenApiUtil { private OpenApiUtil() {} public static Map> mapApiGroups(final JsonNode openAPI, final boolean groupByTags) { - final Map> mapApis = new HashMap(); + final Map> mapApis = new HashMap<>(); final var pathList = openAPI.findValue("paths").fields(); if (pathList.hasNext()) { mapApis.putAll(groupByTags ? mapApiGroupsByTags(pathList) : mapApiGroupsByUrl(openAPI)); @@ -98,16 +104,16 @@ private static Map> mapApiGroupsByUrl(final JsonNo return mapByUrl; } - public static JsonNode getPojoFromSpecFile(final SpecFile specFile) { + public static JsonNode getPojoFromSpecFile(final Path baseDir, final SpecFile specFile) { - return getPojoFromRef(null, specFile.getFilePath()); + return getPojoFromRef(baseDir, specFile.getFilePath()); } - public static JsonNode getPojoFromRef(final String rootFilePath, final String refPath) { + public static JsonNode getPojoFromRef(final Path rootFilePath, final String refPath) { final JsonNode openAPI; try { openAPI = PARSER.readTree(readFile(rootFilePath, refPath)); - } catch (final IOException | URISyntaxException e) { + } catch (final IOException e) { throw new FileParseException(refPath, e); } @@ -118,10 +124,10 @@ public static JsonNode getPojoFromRef(final String rootFilePath, final String re return openAPI; } - private static String readFile(final String rootFilePath, final String filePath) throws MalformedURLException, URISyntaxException { + private static String readFile(final Path rootFilePath, final String filePath) throws MalformedURLException { URL fileURL = OpenApiUtil.class.getClassLoader().getResource(filePath); if (Objects.isNull(fileURL)) { - final var parentFolder = Paths.get(OpenApiUtil.class.getClassLoader().getResource(rootFilePath).toURI()).getParent().resolve(filePath); + final var parentFolder = rootFilePath.resolve(filePath); fileURL = parentFolder.toUri().toURL(); } final var sb = new StringBuilder(); @@ -249,7 +255,7 @@ private static String capLettersAfterSpecialCharacters(final String pathName, fi return javaFileName; } - protected static JsonNode solveRef(final String refValue, final Map schemaMap, final String rootFilePath) { + protected static JsonNode solveRef(final String refValue, final Map schemaMap, final Path rootFilePath) { JsonNode solvedRef; if (StringUtils.isNotEmpty(refValue)) { if (refValue.startsWith("#")) { @@ -258,7 +264,7 @@ protected static JsonNode solveRef(final String refValue, final Map fileSpecToProcess() { diff --git a/scs-multiapi-maven-plugin/src/main/java/com/sngular/api/generator/plugin/OpenapiMultiFileMojo.java b/scs-multiapi-maven-plugin/src/main/java/com/sngular/api/generator/plugin/OpenapiMultiFileMojo.java index a8064b60..058e9b47 100644 --- a/scs-multiapi-maven-plugin/src/main/java/com/sngular/api/generator/plugin/OpenapiMultiFileMojo.java +++ b/scs-multiapi-maven-plugin/src/main/java/com/sngular/api/generator/plugin/OpenapiMultiFileMojo.java @@ -22,8 +22,8 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; -@Mojo(name = "openapi-generation", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE) @Slf4j +@Mojo(name = "openapi-generation", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE) public final class OpenapiMultiFileMojo extends AbstractMojo { @Parameter(defaultValue = "${project}", required = true, readonly = true) @@ -51,7 +51,7 @@ public void execute() throws MojoExecutionException { processGeneratedSourcesFolderName(); addGeneratedSourcesToProject(); final OpenApiGenerator openApiGenerator = new OpenApiGenerator(springBootVersion, overwriteModel, processedGeneratedSourcesFolder, project.getModel().getGroupId(), - targetFolder, project.getBasedir()); + targetFolder, project.getBasedir()); if (null != specFiles && !specFiles.isEmpty()) { openApiGenerator.processFileSpec(specFiles); } else { @@ -75,3 +75,4 @@ private void addGeneratedSourcesToProject() { } + diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiErrorDTO.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiErrorDTO.java index 46ae38e4..4f67cf85 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiErrorDTO.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiErrorDTO.java @@ -59,19 +59,11 @@ public ApiErrorDTO build() { } } - /** - * Get code - * @return code - */ @Schema(name = "code", required = true) public Integer getCode() { return code; } - /** - * Get message - * @return message - */ @Schema(name = "message", required = true) public String getMessage() { return message; @@ -104,10 +96,6 @@ public String toString() { return sb.toString(); } - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ private String toIndentedString(Object o) { if (o == null) { return "null"; @@ -115,7 +103,6 @@ private String toIndentedString(Object o) { return o.toString().replace("\n", "\n "); } - private void validateRequiredAttributes() { boolean satisfiedCondition = true; diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestDTO.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestDTO.java index df2678af..1cffb81f 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestDTO.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestDTO.java @@ -59,19 +59,11 @@ public ApiTestDTO build() { } } - /** - * Get name - * @return name - */ @Schema(name = "name", required = true) public String getName() { return name; } - /** - * Get id - * @return id - */ @Schema(name = "id", required = true) public Integer getId() { return id; @@ -104,10 +96,6 @@ public String toString() { return sb.toString(); } - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ private String toIndentedString(Object o) { if (o == null) { return "null"; @@ -115,7 +103,6 @@ private String toIndentedString(Object o) { return o.toString().replace("\n", "\n "); } - private void validateRequiredAttributes() { boolean satisfiedCondition = true; diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestInfoDTO.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestInfoDTO.java index 03cf436d..ef941c78 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestInfoDTO.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/ApiTestInfoDTO.java @@ -68,10 +68,6 @@ public ApiTestInfoDTO build() { } } - /** - * Get testers - * @return testers - */ @Schema(name = "testers", required = false) public List getTesters() { return testers; @@ -80,10 +76,6 @@ public void setTesters(List testers) { this.testers = testers; } - /** - * Get testName - * @return testName - */ @Schema(name = "testName", required = true) public String getTestName() { return testName; @@ -116,10 +108,6 @@ public String toString() { return sb.toString(); } - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ private String toIndentedString(Object o) { if (o == null) { return "null"; @@ -127,7 +115,6 @@ private String toIndentedString(Object o) { return o.toString().replace("\n", "\n "); } - private void validateRequiredAttributes() { boolean satisfiedCondition = true; diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/TestApi.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/TestApi.java index b5fdf20a..c2b45dab 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/TestApi.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testMultiapiGeneratedSourcesFolder/assets/TestApi.java @@ -23,10 +23,10 @@ public interface TestApi { /** - * GET /test/{testId} : Info for a specific test - * @param testId The id of the test to retrieve true - * @return Expected response to a valid request; (status code 200) - */ + * GET /test/{testId}: Info for a specific test + * @param testId The id of the test to retrieve true + * @return Expected response to a valid request; (status code 200) + */ @Operation( operationId = "showTestById", @@ -47,9 +47,9 @@ default ResponseEntity showTestById(@Parameter(name = "testId", return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } /** - * GET /test : List all available test - * @return A paged array of tests; (status code 200) - */ + * GET /test: List all available test + * @return A paged array of tests; (status code 200) + */ @Operation( operationId = "listTest", diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiErrorDTO.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiErrorDTO.java index 46ae38e4..4f67cf85 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiErrorDTO.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiErrorDTO.java @@ -59,19 +59,11 @@ public ApiErrorDTO build() { } } - /** - * Get code - * @return code - */ @Schema(name = "code", required = true) public Integer getCode() { return code; } - /** - * Get message - * @return message - */ @Schema(name = "message", required = true) public String getMessage() { return message; @@ -104,10 +96,6 @@ public String toString() { return sb.toString(); } - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ private String toIndentedString(Object o) { if (o == null) { return "null"; @@ -115,7 +103,6 @@ private String toIndentedString(Object o) { return o.toString().replace("\n", "\n "); } - private void validateRequiredAttributes() { boolean satisfiedCondition = true; diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestDTO.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestDTO.java index df2678af..1cffb81f 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestDTO.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestDTO.java @@ -59,19 +59,11 @@ public ApiTestDTO build() { } } - /** - * Get name - * @return name - */ @Schema(name = "name", required = true) public String getName() { return name; } - /** - * Get id - * @return id - */ @Schema(name = "id", required = true) public Integer getId() { return id; @@ -104,10 +96,6 @@ public String toString() { return sb.toString(); } - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ private String toIndentedString(Object o) { if (o == null) { return "null"; @@ -115,7 +103,6 @@ private String toIndentedString(Object o) { return o.toString().replace("\n", "\n "); } - private void validateRequiredAttributes() { boolean satisfiedCondition = true; diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestInfoDTO.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestInfoDTO.java index 03cf436d..ef941c78 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestInfoDTO.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestInfoDTO.java @@ -68,10 +68,6 @@ public ApiTestInfoDTO build() { } } - /** - * Get testers - * @return testers - */ @Schema(name = "testers", required = false) public List getTesters() { return testers; @@ -80,10 +76,6 @@ public void setTesters(List testers) { this.testers = testers; } - /** - * Get testName - * @return testName - */ @Schema(name = "testName", required = true) public String getTestName() { return testName; @@ -116,10 +108,6 @@ public String toString() { return sb.toString(); } - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ private String toIndentedString(Object o) { if (o == null) { return "null"; @@ -127,7 +115,6 @@ private String toIndentedString(Object o) { return o.toString().replace("\n", "\n "); } - private void validateRequiredAttributes() { boolean satisfiedCondition = true; diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestsDTO.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestsDTO.java index 072d472c..cb002ad4 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestsDTO.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/ApiTestsDTO.java @@ -35,10 +35,6 @@ public ApiTestsDTO build() { } } - /** - * Get apiTestDTO - * @return apiTestDTO - */ @Schema(name = "apiTestDTO", required = false) public ApiTestDTO getApiTestDTO() { return apiTestDTO; @@ -73,10 +69,6 @@ public String toString() { return sb.toString(); } - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ private String toIndentedString(Object o) { if (o == null) { return "null"; diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/TestApi.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/TestApi.java index b5fdf20a..c2b45dab 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/TestApi.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/multiapi/integration/test/ScsMultiapiGenerationIT/testScsMultiapiGeneration/assets/TestApi.java @@ -23,10 +23,10 @@ public interface TestApi { /** - * GET /test/{testId} : Info for a specific test - * @param testId The id of the test to retrieve true - * @return Expected response to a valid request; (status code 200) - */ + * GET /test/{testId}: Info for a specific test + * @param testId The id of the test to retrieve true + * @return Expected response to a valid request; (status code 200) + */ @Operation( operationId = "showTestById", @@ -47,9 +47,9 @@ default ResponseEntity showTestById(@Parameter(name = "testId", return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } /** - * GET /test : List all available test - * @return A paged array of tests; (status code 200) - */ + * GET /test: List all available test + * @return A paged array of tests; (status code 200) + */ @Operation( operationId = "listTest", diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestFirstApi.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestFirstApi.java index 08ad21a8..0a814ed1 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestFirstApi.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestFirstApi.java @@ -23,9 +23,9 @@ public interface TestFirstApi { /** - * GET /testFirst : List all available test - * @return A paged array of tests; (status code 200) - */ + * GET /testFirst: List all available test + * @return A paged array of tests; (status code 200) + */ @Operation( operationId = "listTest", @@ -46,10 +46,10 @@ default ResponseEntity listTest() { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } /** - * GET /testFirst/{testId} : Info for a specific test - * @param testId The id of the test to retrieve true - * @return Expected response to a valid request; (status code 200) - */ + * GET /testFirst/{testId}: Info for a specific test + * @param testId The id of the test to retrieve true + * @return Expected response to a valid request; (status code 200) + */ @Operation( operationId = "showTestById", diff --git a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestSecondApi.java b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestSecondApi.java index d92dc296..a77b881e 100644 --- a/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestSecondApi.java +++ b/scs-multiapi-maven-plugin/src/test/resources-its/com/sngular/api/generator/openapi/integration/test/OpenApiGenerationIT/testApiMultiGeneration/assets/TestSecondApi.java @@ -23,9 +23,9 @@ public interface TestSecondApi { /** - * GET /testSecond : List all available test - * @return A paged array of tests; (status code 200) - */ + * GET /testSecond: List all available test + * @return A paged array of tests; (status code 200) + */ @Operation( operationId = "listTest", @@ -46,10 +46,10 @@ default ResponseEntity listTest() { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } /** - * GET /testSecond/{testId} : Info for a specific test - * @param testId The id of the test to retrieve true - * @return Expected response to a valid request; (status code 200) - */ + * GET /testSecond/{testId}: Info for a specific test + * @param testId The id of the test to retrieve true + * @return Expected response to a valid request; (status code 200) + */ @Operation( operationId = "showTestById",