Skip to content

Commit

Permalink
#160 Fix creation problems for AnyOf, OneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jun 20, 2023
1 parent a2932e9 commit 4b0f8d9
Show file tree
Hide file tree
Showing 44 changed files with 3,269 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -202,7 +203,7 @@ private void createModelTemplate(final SpecFile specFile, final JsonNode openAPI
final var modelPackage = processModelPackage(specFile.getModelPackage());
final var basicSchemaMap = OpenApiUtil.processBasicJsonNodes(openAPI, globalObject.getSchemaMap());
templateFactory.setModelPackageName(modelPackage);
processModels(specFile, openAPI, fileModelToSave, modelPackage, basicSchemaMap, Boolean.TRUE.equals(overwriteModel));
processModels(specFile, fileModelToSave, modelPackage, basicSchemaMap, Boolean.TRUE.equals(overwriteModel));
}

private void processPackage(final String apiPackage) {
Expand Down Expand Up @@ -250,49 +251,48 @@ private String convertPackageToTargetPath(final String fileSpecPackage, final bo
}

private void processModels(
final SpecFile specFile, final JsonNode openAPI, final String fileModelToSave, final String modelPackage, final Map<String, JsonNode> basicSchemaMap,
final boolean overwrite) {

final SpecFile specFile, final String fileModelToSave, final String modelPackage, final Map<String, JsonNode> basicSchemaMap,
final boolean overwrite) {
final Map<String, SchemaObject> builtSchemasMap = new HashMap<>();
basicSchemaMap.forEach((schemaName, basicSchema) -> {
if (!overwrite && !overwriteModelList.add(schemaName + modelPackage)) {
throw new DuplicateModelClassException(schemaName, modelPackage);
}

if (ApiTool.hasRef(basicSchema)) {
final var refSchema = MapperUtil.getRefSchemaName(basicSchema);
writeModel(specFile, openAPI, fileModelToSave, refSchema, basicSchemaMap.get(refSchema));
builtSchemasMap.putAll(writeModel(specFile, fileModelToSave, refSchema, basicSchemaMap.get(refSchema), basicSchemaMap, builtSchemasMap));
} else if (!ApiTool.isArray(basicSchema) && !TypeConstants.STRING.equalsIgnoreCase(ApiTool.getType(basicSchema))) {
writeModel(specFile, openAPI, fileModelToSave, schemaName, basicSchema);
builtSchemasMap.putAll(writeModel(specFile, fileModelToSave, schemaName, basicSchema, basicSchemaMap, builtSchemasMap));
}
});

}

private void writeModel(final SpecFile specFile, final JsonNode openAPI, final String fileModelToSave, final String schemaName, final JsonNode basicSchema) {
final var schemaObjectList = MapperContentUtil.mapComponentToSchemaObject(ApiTool.getComponentSchemas(openAPI), basicSchema, schemaName, specFile);
final Set<String> propertiesSet = new HashSet<>();
checkRequiredOrCombinatorExists(schemaObjectList);
schemaObjectList.values().forEach(schemaObject -> {
private Map<String, SchemaObject> writeModel(final SpecFile specFile, final String fileModelToSave,
final String schemaName, final JsonNode basicSchema,
final Map<String, JsonNode> basicSchemaMap, final Map<String, SchemaObject> builtSchemasMap) {
final var schemaObjectMap = MapperContentUtil
.mapComponentToSchemaObject(basicSchemaMap, builtSchemasMap, basicSchema, schemaName, specFile, baseDir);
checkRequiredOrCombinatorExists(schemaObjectMap);
schemaObjectMap.values().forEach(schemaObject -> {
try {
final Set<String> propertiesSet = new HashSet<>();
templateFactory.fillTemplateSchema(fileModelToSave, specFile.isUseLombokModelAnnotation(), schemaObject, propertiesSet);
fillTemplates(fileModelToSave, propertiesSet);
} catch (IOException | TemplateException e) {
throw new GeneratedSourcesException(fileModelToSave, e);
throw new GeneratedSourcesException(schemaObject.getClassName(), e);
}
});

try {
fillTemplates(fileModelToSave, propertiesSet);
} catch (IOException | TemplateException e) {
throw new GeneratedSourcesException(fileModelToSave, e);
}

if (Boolean.TRUE.equals(generateExceptionTemplate)) {
try {
templateFactory.fillTemplateModelClassException(fileModelToSave, true);
} catch (IOException | TemplateException e) {
throw new GeneratedSourcesException(fileModelToSave, e);
}
}
return schemaObjectMap;
}

private void fillTemplates(final String filePathToSave, final Set<String> fieldProperties) throws TemplateException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Data
@Builder
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@

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

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@Builder
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = {"schemaName", "className"})
public class SchemaObject {

private boolean isEnum;
Expand All @@ -36,7 +38,7 @@ public class SchemaObject {

public static final class SchemaObjectBuilder {

private boolean isEnum = false;
private boolean isEnum;

private final List<String> importList = new ArrayList<>();

Expand All @@ -52,7 +54,7 @@ public SchemaObjectBuilder importItem(final String importItem) {
return this;
}

public SchemaObjectBuilder fieldObjectList(final List<SchemaFieldObject> fieldObjectList) {
public SchemaObjectBuilder fieldObjectList(final Set<SchemaFieldObject> fieldObjectList) {
this.fieldObjectList.addAll(fieldObjectList);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public TemplateFactory() {

}

public final void fillTemplateSchema(final String filePathToSave, final Boolean useLombok, final SchemaObject schemaObject, final Set<String> propertiesSet) throws IOException,
public final void fillTemplateSchema(
final String filePathToSave, final Boolean useLombok, final SchemaObject schemaObject,
final Set<String> propertiesSet) throws IOException,
TemplateException {
final File fileToSave = new File(filePathToSave);
if (Objects.nonNull(schemaObject.getFieldObjectList()) && !schemaObject.getFieldObjectList().isEmpty()) {
Expand Down Expand Up @@ -148,7 +150,7 @@ public final void fillTemplate(

}

public void calculateJavaEEPackage(final Integer springBootVersion) {
public final void calculateJavaEEPackage(final Integer springBootVersion) {
if (3 <= springBootVersion) {
root.put("javaEEPackage", "jakarta");
} else {
Expand Down
Loading

0 comments on commit 4b0f8d9

Please sign in to comment.