Skip to content

Commit

Permalink
#160 Fix reference solving
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jun 6, 2023
1 parent 2a45186 commit c976095
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 206 deletions.
Original file line number Diff line number Diff line change
@@ -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() {
}

Expand All @@ -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) {
Expand Down Expand Up @@ -80,10 +95,10 @@ public static JsonNode getItems(final JsonNode schema) {
public static Map<String, JsonNode> getComponentSchemas(final JsonNode openApi) {
final var schemasMap = new HashMap<String, JsonNode>();

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)));
}
Expand All @@ -95,8 +110,8 @@ public static Map<String, JsonNode> getComponentSchemas(final JsonNode openApi)
public static Map<String, JsonNode> getComponentSecuritySchemes(final JsonNode openApi) {
final var schemasMap = new HashMap<String, JsonNode>();

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()));
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -230,8 +245,8 @@ public static List<JsonNode> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class OpenApiGenerator {

private final File targetFolder;

private final File baseDir;
private final Path baseDir;

private Boolean isWebClient = false;

Expand All @@ -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;
}
Expand All @@ -111,7 +111,7 @@ public final void processFileSpec(final List<SpecFile> 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()) {
Expand Down Expand Up @@ -169,7 +169,7 @@ private GlobalObject createApiTemplate(final SpecFile specFile, final String fil

for (Map.Entry<String, Map<String, JsonNode>> apisEntry : apis.entrySet()) {
final String javaFileName = OpenApiUtil.processJavaFileName(apisEntry.getKey());
final List<PathObject> pathObjects = MapperPathUtil.mapPathObjects(openAPI, specFile, apisEntry, globalObject);
final List<PathObject> pathObjects = MapperPathUtil.mapPathObjects(openAPI, specFile, apisEntry, globalObject, baseDir);
final AuthObject authObject = MapperAuthUtil.getApiAuthObject(globalObject.getAuthSchemas(), pathObjects);

try {
Expand Down Expand Up @@ -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());
Expand Down
Loading

0 comments on commit c976095

Please sign in to comment.