Skip to content

Commit

Permalink
#251 Fix Parameter without description
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Aug 8, 2023
1 parent 370ac0c commit 640feef
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
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.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.Map.Entry;

public final class ApiTool {

public static final String FORMAT = "format";
Expand Down Expand Up @@ -96,43 +89,13 @@ public static JsonNode getItems(final JsonNode schema) {
return getNode(schema, "items");
}

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);
final var schemasIt = schemas.fieldNames();
schemasIt.forEachRemaining(name -> schemasMap.put(name, getNode(schemas, name)));
}
}

return schemasMap;
}

public static Map<String, JsonNode> getParameterSchemas(final JsonNode openApi) {
final var schemasMap = new HashMap<String, JsonNode>();

if (hasNode(openApi, COMPONENTS)) {
final var components = getNode(openApi, COMPONENTS);
if (hasNode(components, PARAMETERS)) {
final var schemas = getNode(components, PARAMETERS);
final var schemasIt = schemas.fieldNames();
schemasIt.forEachRemaining(name -> schemasMap.put(name, getNode(schemas, name)));
}
}

return schemasMap;
}

public static Map<String, JsonNode> getResponseSchemas(final JsonNode openApi) {
public static Map<String, JsonNode> getComponentTypeSchemas(final JsonNode openApi, String nodeType) {
final var schemasMap = new HashMap<String, JsonNode>();

if (hasNode(openApi, COMPONENTS)) {
final var components = getNode(openApi, COMPONENTS);
if (hasNode(components, RESPONSES)) {
final var schemas = getNode(components, RESPONSES);
if (hasNode(components, nodeType)) {
final var schemas = getNode(components, nodeType);
final var schemasIt = schemas.fieldNames();
schemasIt.forEachRemaining(name -> schemasMap.put(name, getNode(schemas, name)));
}
Expand Down Expand Up @@ -243,7 +206,7 @@ public static boolean isNumber(final JsonNode schema) {
}

public static boolean isEnum(final JsonNode schema) {
return schema.has("enum");
return hasNode(schema, "enum");
}

public static boolean isAllOf(final JsonNode schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,30 @@

package com.sngular.api.generator.plugin.openapi;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

import com.fasterxml.jackson.databind.JsonNode;
import com.sngular.api.generator.plugin.PluginConstants;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.exception.GeneratedSourcesException;
import com.sngular.api.generator.plugin.exception.GeneratorTemplateException;
import com.sngular.api.generator.plugin.openapi.exception.CodeGenerationException;
import com.sngular.api.generator.plugin.openapi.exception.DuplicateModelClassException;
import com.sngular.api.generator.plugin.openapi.model.AuthObject;
import com.sngular.api.generator.plugin.openapi.model.GlobalObject;
import com.sngular.api.generator.plugin.openapi.model.PathObject;
import com.sngular.api.generator.plugin.openapi.model.SchemaObject;
import com.sngular.api.generator.plugin.openapi.model.TypeConstants;
import com.sngular.api.generator.plugin.openapi.model.*;
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import com.sngular.api.generator.plugin.openapi.template.TemplateFactory;
import com.sngular.api.generator.plugin.openapi.template.TemplateIndexConstants;
import com.sngular.api.generator.plugin.openapi.utils.MapperAuthUtil;
import com.sngular.api.generator.plugin.openapi.utils.MapperContentUtil;
import com.sngular.api.generator.plugin.openapi.utils.MapperPathUtil;
import com.sngular.api.generator.plugin.openapi.utils.MapperUtil;
import com.sngular.api.generator.plugin.openapi.utils.OpenApiUtil;
import com.sngular.api.generator.plugin.openapi.utils.*;
import freemarker.template.TemplateException;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Pattern;

public class OpenApiGenerator {

private static final String DEFAULT_OPENAPI_API_PACKAGE = PluginConstants.DEFAULT_API_PACKAGE + ".openapi";
Expand Down Expand Up @@ -262,7 +248,7 @@ private void processModels(

if (ApiTool.hasRef(basicSchema)) {
final var refSchema = MapperUtil.getRefSchemaName(basicSchema);
builtSchemasMap.putAll(writeModel(specFile, fileModelToSave, refSchema, basicSchemaMap.get(refSchema), basicSchemaMap, builtSchemasMap));
builtSchemasMap.putAll(writeModel(specFile, fileModelToSave, refSchema, basicSchemaMap.getOrDefault(refSchema, basicSchema), basicSchemaMap, builtSchemasMap));
} else if (!ApiTool.isArray(basicSchema) && !TypeConstants.STRING.equalsIgnoreCase(ApiTool.getType(basicSchema))) {
builtSchemasMap.putAll(writeModel(specFile, fileModelToSave, schemaName, basicSchema, basicSchemaMap, builtSchemasMap));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@

package com.sngular.api.generator.plugin.openapi.utils;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

import com.fasterxml.jackson.databind.JsonNode;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.openapi.exception.BadDefinedEnumException;
Expand All @@ -28,6 +17,10 @@
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;

public class MapperContentUtil {

private static final String ADDITIONAL_PROPERTY_NAME = "AdditionalProperty";
Expand Down Expand Up @@ -56,7 +49,10 @@ private static Map<String, SchemaObject> mapComponentToSchemaObject(
final List<String> antiLoopList, final JsonNode schema, final String schemaName, final SpecFile specFile,
final Path baseDir) {
final var name = StringUtils.defaultIfBlank(ApiTool.getNodeAsString(schema, "name"), schemaName);
if (!compositedSchemas.containsKey(name)) {
if (ApiTool.hasRef(schema)) {
compositedSchemas
.put(name, solveRef(schema, totalSchemas, compositedSchemas, antiLoopList, specFile, baseDir));
} else if (!compositedSchemas.containsKey(name)) {
compositedSchemas
.put(name, toSchemaObject(name, totalSchemas, compositedSchemas, antiLoopList, schema, schemaName, specFile, baseDir));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,21 @@

package com.sngular.api.generator.plugin.openapi.utils;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
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 java.util.function.BiConsumer;

import com.fasterxml.jackson.databind.JsonNode;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.openapi.exception.DuplicatedOperationException;
import com.sngular.api.generator.plugin.openapi.exception.InvalidOpenAPIException;
import com.sngular.api.generator.plugin.openapi.model.AuthSchemaObject;
import com.sngular.api.generator.plugin.openapi.model.ContentObject;
import com.sngular.api.generator.plugin.openapi.model.GlobalObject;
import com.sngular.api.generator.plugin.openapi.model.*;
import com.sngular.api.generator.plugin.openapi.model.GlobalObject.GlobalObjectBuilder;
import com.sngular.api.generator.plugin.openapi.model.OperationObject;
import com.sngular.api.generator.plugin.openapi.model.ParameterObject;
import com.sngular.api.generator.plugin.openapi.model.PathObject;
import com.sngular.api.generator.plugin.openapi.model.RequestObject;
import com.sngular.api.generator.plugin.openapi.model.ResponseObject;
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 java.nio.file.Path;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.BiConsumer;

public class MapperPathUtil {

public static final String INLINE_PARAMETER = "InlineParameter";
Expand All @@ -52,9 +37,9 @@ public static GlobalObject mapOpenApiObjectToOurModels(final JsonNode openAPI, f
final GlobalObjectBuilder globalObject =
GlobalObject.builder().url(ApiTool.getNode(openAPI, "servers").findValue("url").textValue()).authSchemas(authSchemaList).authentications(authList);
if (ApiTool.hasNode(openAPI, "components")) {
globalObject.schemaMap(ApiTool.getComponentSchemas(openAPI));
globalObject.parameterMap(ApiTool.getParameterSchemas(openAPI));
globalObject.responseMap(ApiTool.getResponseSchemas(openAPI));
globalObject.schemaMap(ApiTool.getComponentTypeSchemas(openAPI, ApiTool.SCHEMAS));
globalObject.parameterMap(ApiTool.getComponentTypeSchemas(openAPI, ApiTool.PARAMETERS));
globalObject.responseMap(ApiTool.getComponentTypeSchemas(openAPI, ApiTool.RESPONSES));
} else {
globalObject.schemaMap(new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@

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;
Expand All @@ -35,6 +19,15 @@
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.URL;
import java.nio.file.Path;
import java.util.*;
import java.util.Map.Entry;

public class OpenApiUtil {

public static final String PATHS = "paths";
Expand Down Expand Up @@ -253,7 +246,7 @@ protected static JsonNode solveRef(final String refValue, final Map<String, Json
final var filePath = refValueArr[0];
solvedRef = getPojoFromRef(rootFilePath.toAbsolutePath(), filePath);
final var refName = MapperUtil.getRefSchemaName(refValueArr[1]);
schemaMap.putAll(ApiTool.getComponentSchemas(solvedRef));
schemaMap.putAll(ApiTool.getComponentTypeSchemas(solvedRef, ApiTool.SCHEMAS));
solvedRef = solvedRef.findValue(refName);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,6 @@ components:
type: array
items:
type: string
defaultValue:
type: string
enumValues:
type: array
items:
Expand Down Expand Up @@ -370,10 +368,9 @@ components:
defaultValues:
type: array
items:
type: object
type: number
defaultValue:
type: integer
format: int64
type: number
minimum:
type: integer
format: int32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ public class EnumFieldDTO {
@JsonProperty(value ="name")
private String name;

@JsonProperty(value ="defaultValue")
private String defaultValue;


@Builder
@Jacksonized
private EnumFieldDTO(List<String> enumValues, String type, List<String> defaultValues, String name, String defaultValue) {
private EnumFieldDTO(List<String> enumValues, String type, List<String> defaultValues, String name) {
this.enumValues = enumValues;
this.type = type;
this.defaultValues = defaultValues;
this.name = name;
this.defaultValue = defaultValue;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.List;
import java.util.ArrayList;
import java.math.BigDecimal;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;
Expand All @@ -21,7 +22,7 @@ public class NumberFieldDTO {
private String type;

@JsonProperty(value ="defaultValues")
private List<Object> defaultValues = new ArrayList<Object>();
private List<BigDecimal> defaultValues = new ArrayList<BigDecimal>();

@JsonProperty(value ="name")
private String name;
Expand Down Expand Up @@ -66,12 +67,12 @@ public String toString() {
}

@JsonProperty(value ="defaultValue")
private Long defaultValue;
private BigDecimal defaultValue;


@Builder
@Jacksonized
private NumberFieldDTO(Integer precision, Integer maximum, String type, List<Object> defaultValues, String name, Integer multipleOf, Integer scale, Integer minimum, Boolean flagExclusiveMinimum, Boolean flagExclusiveMaximum, NumberEnum numberEnum, Long defaultValue) {
private NumberFieldDTO(Integer precision, Integer maximum, String type, List<BigDecimal> defaultValues, String name, Integer multipleOf, Integer scale, Integer minimum, Boolean flagExclusiveMinimum, Boolean flagExclusiveMaximum, NumberEnum numberEnum, BigDecimal defaultValue) {
this.precision = precision;
this.maximum = maximum;
this.type = type;
Expand Down

0 comments on commit 640feef

Please sign in to comment.