Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -225,137 +225,7 @@ public void generate(final String targetPath, Object model, Any config) throws I
ArrayList<Runnable> modelCreators = new ArrayList<>();
final HashMap<String, Any> references = new HashMap<>();
for (Map.Entry<String, Any> entry : schemas.asMap().entrySet()) {
final List<Map<String, Any>> props = new ArrayList<>();
String key = entry.getKey();
Map<String, Any> value = entry.getValue().asMap();
String type = null;
String enums = null;
boolean isEnum = false;
boolean isEnumClass = false;
// Map<String, Any> properties = null;
List<Any> required = null;

// iterate through each schema in the components
for (Map.Entry<String, Any> entrySchema : value.entrySet()) {
if ("type".equals(entrySchema.getKey())) {
type = entrySchema.getValue().toString();
if ("enum".equals(type)) {
isEnum = true;
}
}
if ("enum".equals(entrySchema.getKey())) {
isEnumClass = true;
enums = entrySchema.getValue().asList().toString();
enums = enums.substring(enums.indexOf("[") + 1, enums.indexOf("]"));
}
if ("properties".equals(entrySchema.getKey())) {
handleProperties(props, entrySchema.getValue().asMap());
}
if ("required".equals(entrySchema.getKey())) {
required = entrySchema.getValue().asList();
}
if ("allOf".equals(entrySchema.getKey())) {
type = "object";

// could be referred to as "$ref" references or listed in "properties"
for (Any listItem : entrySchema.getValue().asList()) {
//Map<String, Any> allOfItem = (Map<String, Any>)listItem.asMap().entrySet();

for (Map.Entry<String, Any> allOfItem : listItem.asMap().entrySet()) {
if ("$ref".equals(allOfItem.getKey())) {
String s = allOfItem.getValue().toString();
s = s.substring(s.lastIndexOf('/') + 1);
if (schemas.get(s).keys().contains("properties")) {
handleProperties(props, schemas.get(s).get("properties").asMap());
}
}
if ("properties".equals(allOfItem.getKey())) {
handleProperties(props, allOfItem.getValue().asMap());
}
}
}
}
}
final String classVarName = key;
final String modelFileName = key.substring(0, 1).toUpperCase() + key.substring(1);
//System.out.println("props = " + Any.wrap(props));

// Check the type of current schema. Generation will be executed only if the type of the schema equals to object.
// Since generate a model for primitive types and arrays do not make sense, and an error class would be generated
// due to lack of properties if force to generate.
if (type == null) {
throw new RuntimeException("Cannot find the type of \"" + modelFileName + "\" in #/components/schemas/ of the specification file.");
}

if ("object".equals(type) || isEnumClass) {
if (!overwriteModel && checkExist(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java")) {
continue;
}

final String enumsIfClass = isEnumClass ? enums : null;
modelCreators.add(() -> {
final int referencesCount = references.size();
for (Map<String, Any> properties : props) {
Any any = properties.get("type");
if (any != null) {
if (any.valueType() == ValueType.STRING) {
Any resolved = references.get(any.toString());
if (resolved == null) {
continue;
}
any = new UnresolvedTypeHolderAny(resolved);
properties.put("type", any);
}

int iteration = 0;
do {
UnresolvedTypeAny previous = null;
while (any instanceof UnresolvedTypeAny) {
previous = (UnresolvedTypeAny)any;
any = ((UnresolvedTypeAny)any).get();
}

if (any == null) {
break;
} else if (iteration++ > referencesCount) {
throw new TypeNotPresentException(any.toString(), null);
}

if (any.valueType() == ValueType.STRING) {
any = references.get(any.toString());
if (any == null) {
break;
} else {
previous.set(any);
}
} else {
break;
}
} while (true);
}
}

try {
transfer(targetPath,
("src.main.java." + modelPackage).replace(".", separator),
modelFileName + ".java",
enumsIfClass == null
? templates.rest.pojo.template(modelPackage, modelFileName, classVarName, props)
: templates.rest.enumClass.template(modelPackage, modelFileName, enumsIfClass));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
} else {
HashMap<String, Any> map = new HashMap<>(1);
map.put(key, Any.wrap(value));
handleProperties(props, map);
if (props.isEmpty()) {
throw new IllegalStateException("Properties empty for " + classVarName + "!");
}

references.put(modelFileName, props.get(0).get("type"));
}
loadModel(entry.getKey(), null, entry.getValue().asMap(), schemas, overwriteModel, targetPath, modelPackage, modelCreators, references, null);
}

for (Runnable r : modelCreators) {
Expand Down Expand Up @@ -926,4 +796,159 @@ private Map<String, String> populateResponseExample(Operation operation) {

private static final Logger logger = LoggerFactory.getLogger(OpenApiGenerator.class);

private void loadModel(String classVarName, String parentClassName, Map<String, Any> value, Any schemas, boolean overwriteModel, String targetPath, String modelPackage, List<Runnable> modelCreators, Map<String, Any> references, List<Map<String, Any>> parentClassProps) throws IOException {
final String modelFileName = classVarName.substring(0, 1).toUpperCase() + classVarName.substring(1);
final List<Map<String, Any>> props = new ArrayList<>();
final List<Map<String, Any>> parentProps = (parentClassProps == null) ? new ArrayList<>() : new ArrayList<>(parentClassProps);
String type = null;
String enums = null;
boolean isEnumClass = false;
List<Any> required = null;
boolean isAbstractClass = false;

// iterate through each schema in the components
Queue<Map.Entry<String, Any>> schemaElementQueue = new LinkedList<>();
// cache the visited elements to prevent loop reference
Set<String> seen = new HashSet<>();
// add elements into queue to perform a BFS
for (Map.Entry<String, Any> entrySchema : value.entrySet()) {
schemaElementQueue.offer(entrySchema);
}
while (!schemaElementQueue.isEmpty()) {
Map.Entry<String, Any> currentElement = schemaElementQueue.poll();
String currentElementKey = currentElement.getKey();
// handle the base elements
if ("type".equals(currentElementKey) && type == null) {
type = currentElement.getValue().toString();
}
if ("enum".equals(currentElementKey)) {
isEnumClass = true;
enums = currentElement.getValue().asList().toString();
enums = enums.substring(enums.indexOf("[") + 1, enums.indexOf("]"));
}
if ("properties".equals(currentElementKey)) {
handleProperties(props, currentElement.getValue().asMap());
}
if ("required".equals(currentElementKey)) {
if (required == null) {
required = new ArrayList<>();
}
required.addAll(currentElement.getValue().asList());
}
// expend the ref elements and add to the queue
if ("$ref".equals(currentElementKey)) {
String s = currentElement.getValue().toString();
s = s.substring(s.lastIndexOf('/') + 1);
if (seen.contains(s)) continue;
seen.add(s);
for (Map.Entry<String, Any> schema : schemas.get(s).asMap().entrySet()) {
schemaElementQueue.offer(schema);
}
}
// expand the allOf elements and add to the queue
if ("allOf".equals(currentElementKey)) {
for (Any listItem : currentElement.getValue().asList()) {
for (Map.Entry<String, Any> allOfItem : listItem.asMap().entrySet()) {
schemaElementQueue.offer(allOfItem);
}
}
}
// call loadModel recursively to generate new model corresponding to each oneOf elements
if ("oneOf".equals(currentElementKey)) {
isAbstractClass = true;
parentProps.addAll(props);
String parentName = classVarName.substring(0, 1) + classVarName.substring(1);
for (Any listItem : currentElement.getValue().asList()) {
for (Map.Entry<String, Any> oneOfItem : listItem.asMap().entrySet()) {
if ("$ref".equals(oneOfItem.getKey())) {
String s = oneOfItem.getValue().toString();
s = s.substring(s.lastIndexOf('/') + 1);
loadModel(extendModelName(s, classVarName), parentName, schemas.get(s).asMap(), schemas, overwriteModel, targetPath, modelPackage, modelCreators, references, parentProps);
}
}
}
}
}

// Check the type of current schema. Generation will be executed only if the type of the schema equals to object.
// Since generate a model for primitive types and arrays do not make sense, and an error class would be generated
// due to lack of properties if force to generate.
if (type == null) {
throw new RuntimeException("Cannot find the type of \"" + modelFileName + "\" in #/components/schemas/ of the specification file.");
}

if ("object".equals(type) || isEnumClass) {
if (!overwriteModel && checkExist(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java")) {
return;
}

final String enumsIfClass = isEnumClass ? enums : null;
final boolean abstractIfClass = isAbstractClass;
modelCreators.add(() -> {
final int referencesCount = references.size();
for (Map<String, Any> properties : props) {
Any any = properties.get("type");
if (any != null) {
if (any.valueType() == ValueType.STRING) {
Any resolved = references.get(any.toString());
if (resolved == null) {
continue;
}
any = new UnresolvedTypeHolderAny(resolved);
properties.put("type", any);
}

int iteration = 0;
do {
UnresolvedTypeAny previous = null;
while (any instanceof UnresolvedTypeAny) {
previous = (UnresolvedTypeAny)any;
any = ((UnresolvedTypeAny)any).get();
}

if (any == null) {
break;
} else if (iteration++ > referencesCount) {
throw new TypeNotPresentException(any.toString(), null);
}

if (any.valueType() == ValueType.STRING) {
any = references.get(any.toString());
if (any == null) {
break;
} else {
previous.set(any);
}
} else {
break;
}
} while (true);
}
}

try {
transfer(targetPath,
("src.main.java." + modelPackage).replace(".", separator),
modelFileName + ".java",
enumsIfClass == null
? templates.rest.pojo.template(modelPackage, modelFileName, parentClassName, classVarName, abstractIfClass, props, parentClassProps)
: templates.rest.enumClass.template(modelPackage, modelFileName, enumsIfClass));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
} else {
HashMap<String, Any> map = new HashMap<>(1);
map.put(classVarName, Any.wrap(value));
handleProperties(props, map);
if (props.isEmpty()) {
throw new IllegalStateException("Properties empty for " + classVarName + "!");
}

references.put(modelFileName, props.get(0).get("type"));
}
}
private String extendModelName(String str1, String str2) {
return str1 + str2.substring(0, 1).toUpperCase() + str2.substring(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
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));
transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.pojo.template(modelPackage, modelFileName, null, classVarName, false, props, null));
}
}

Expand Down
Loading