Skip to content

Commit

Permalink
309 fail to execute a file (#310)
Browse files Browse the repository at this point in the history
* 309 Fix wrong schema generation
  • Loading branch information
jemacineiras committed Feb 19, 2024
1 parent 981695c commit 8fab074
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 57 deletions.
2 changes: 1 addition & 1 deletion multiapi-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sngular</groupId>
<artifactId>multiapi-engine</artifactId>
<version>5.3.0</version>
<version>5.3.1</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ public static String formatTypeOfString(final String format, final TimeType useT
String type = "String";
if (format != null) {
if (DATE_TIME.equalsIgnoreCase(format)) {
switch(useTimeType) {
case ZONED:
type = ZONED_DATE_TIME;
break;
default:
type = LOCAL_DATE_TIME;
if (Objects.requireNonNull(useTimeType) == TimeType.ZONED) {
type = ZONED_DATE_TIME;
} else {
type = LOCAL_DATE_TIME;
}

} else if (DATE.equalsIgnoreCase(format)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@

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;
Expand All @@ -27,18 +40,6 @@
import com.sngular.api.generator.plugin.openapi.utils.MapperUtil;
import com.sngular.api.generator.plugin.openapi.utils.OpenApiUtil;
import freemarker.template.TemplateException;
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 org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -302,17 +303,19 @@ private void processModels(
final Map<String, JsonNode> basicSchemaMap,
final boolean overwrite) {
final Map<String, SchemaObject> builtSchemasMap = new HashMap<>();
basicSchemaMap.forEach(
(schemaName, basicSchema) ->
processModel(
specFile,
fileModelToSave,
modelPackage,
basicSchemaMap,
overwrite,
schemaName,
basicSchema,
builtSchemasMap));
basicSchemaMap.forEach((schemaName, basicSchema) -> {
if (ApiTool.hasType(basicSchema)) {
if (validType(ApiTool.getType(basicSchema))) {
processModel(specFile, fileModelToSave, modelPackage, basicSchemaMap, overwrite, schemaName, basicSchema, builtSchemasMap);
}
} else {
processModel(specFile, fileModelToSave, modelPackage, basicSchemaMap, overwrite, schemaName, basicSchema, builtSchemasMap);
}
});
}

private boolean validType(final String type) {
return !TypeConstants.NO_PROCESS_TYPE.contains(type);
}

private void processModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ public final class TypeConstants {

public static final Set<String> NO_IMPORT_TYPE = Set.of(STRING, INTEGER, OBJECT);

public static final Set<String> NO_PROCESS_TYPE = Set.of(NUMBER,
BOOLEAN,
BIG_DECIMAL,
INTEGER,
DOUBLE,
FLOAT,
LONG,
STRING,
ENUM,
LOCALDATE,
LOCALDATETIME,
ZONEDDATE,
ZONEDDATETIME,
OFFSETDATE,
OFFSETDATETIME);
public static final Set<String> ALL_TYPES = Set.of(
NUMBER,
BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@

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

import com.sngular.api.generator.plugin.openapi.exception.OverwritingApiFilesException;
import com.sngular.api.generator.plugin.openapi.model.AuthObject;
import com.sngular.api.generator.plugin.openapi.model.PathObject;
import com.sngular.api.generator.plugin.openapi.model.SchemaFieldObject;
import com.sngular.api.generator.plugin.openapi.model.SchemaObject;
import com.sngular.api.generator.plugin.openapi.parameter.OpenAPISpecFile;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -27,6 +17,17 @@
import java.util.Objects;
import java.util.Set;

import com.sngular.api.generator.plugin.openapi.exception.OverwritingApiFilesException;
import com.sngular.api.generator.plugin.openapi.model.AuthObject;
import com.sngular.api.generator.plugin.openapi.model.PathObject;
import com.sngular.api.generator.plugin.openapi.model.SchemaFieldObject;
import com.sngular.api.generator.plugin.openapi.model.SchemaObject;
import com.sngular.api.generator.plugin.openapi.parameter.OpenAPISpecFile;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

public class TemplateFactory {

public static final String JAVA_EXTENSION = ".java";
Expand Down Expand Up @@ -105,7 +106,7 @@ public final void fillTemplateRestClient(final String filePathToSave) throws IOE

}

public final void fillTemplateAuth(final String filePathToSave, final String authName) throws IOException, TemplateException {
public final void fillTemplateAuth(final String filePathToSave, final String authName) throws IOException {
final File fileToSave = new File(filePathToSave);
final var nameAuthClass = authName + JAVA_EXTENSION;
final String pathToSaveMainClass = fileToSave.toPath().resolve(nameAuthClass).toString();
Expand All @@ -115,7 +116,7 @@ public final void fillTemplateAuth(final String filePathToSave, final String aut

public final void fillTemplateCustom(
final String filePathToSave, final String annotationFileName, final String validatorFileName, final String annotationTemplate,
final String validatorTemplate) throws IOException, TemplateException {
final String validatorTemplate) throws IOException {
final File fileToSave = new File(filePathToSave);
final Path pathToValidatorPackage = fileToSave.toPath().resolve("customvalidator");
pathToValidatorPackage.toFile().mkdirs();
Expand Down Expand Up @@ -157,18 +158,20 @@ public final void calculateJavaEEPackage(final Integer springBootVersion) {
}
}

private void writeTemplateToFile(final String templateName, final Map<String, Object> root, final String path) throws IOException, TemplateException {
private void writeTemplateToFile(final String templateName, final Map<String, Object> root, final String path) throws IOException {
writeTemplateToFile(templateName, root, path, true);
}

private void writeTemplateToFile(final String templateName, final Map<String, Object> root, final String path, final boolean checkOverwrite) throws IOException,
TemplateException {
private void writeTemplateToFile(final String templateName, final Map<String, Object> root, final String path, final boolean checkOverwrite) throws IOException {
final Template template = cfg.getTemplate(templateName);

if (!Files.exists(Path.of(path)) || checkOverwrite) {
final FileWriter writer = new FileWriter(path);
template.process(root, writer);
writer.close();
try (FileWriter writer = new FileWriter(path)) {
template.process(root, writer);
} catch (IOException | TemplateException exception) {
final var schema = root.get("schema");
throw new GeneratorTemplateException(String.format(" Error processing template %s with object %s", templateName, ((SchemaObject) schema).getClassName()), exception);
}
} else {
throw new OverwritingApiFilesException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.sngular.api.generator.plugin.openapi.model.TypeConstants.TimeType;
import com.sngular.api.generator.plugin.openapi.parameter.OpenAPISpecFile;
import com.sngular.api.generator.test.utils.TestUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import com.sngular.api.generator.plugin.openapi.model.TypeConstants.TimeType;
import com.sngular.api.generator.plugin.openapi.parameter.OpenAPISpecFile;
import com.sngular.api.generator.test.utils.TestUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

Expand Down Expand Up @@ -377,8 +378,20 @@ public final class OpenApiGeneratorFixtures {
.build()
);

static final List<OpenAPISpecFile> TEST_CREATE_BASIC_DTO = List.of(
OpenAPISpecFile
.builder()
.filePath("openapigenerator/testCreateBasicDTO/api-test.yml")
.apiPackage("com.sngular.multifileplugin.testCreateBasicDTO")
.modelPackage("com.sngular.multifileplugin.testCreateBasicDTO.model")
.clientPackage("com.sngular.multifileplugin.testCreateBasicDTO.client")
.modelNameSuffix("DTO")
.useLombokModelAnnotation(true)
.build()
);

static final List<OpenAPISpecFile> TEST_ISSUE_FAKER = List.of(
OpenAPISpecFile
OpenAPISpecFile
.builder()
.filePath("openapigenerator/testIssueFaker/api-test.yml")
.apiPackage("com.sngular.multifileplugin.testissuefaker")
Expand Down Expand Up @@ -1280,6 +1293,30 @@ static Function<Path, Boolean> validateCreateDTO() {
return (path) -> commonTest(path, expectedTestApiFile, expectedTestApiModelFiles, DEFAULT_TARGET_API, DEFAULT_MODEL_API, Collections.emptyList(), DEFAULT_EXCEPTION_API);
}

static Function<Path, Boolean> validateCreateBasicDTO() {

final String DEFAULT_TARGET_API = "generated/com/sngular/multifileplugin/testCreateBasicDTO";

final String DEFAULT_MODEL_API = "generated/com/sngular/multifileplugin/testCreateBasicDTO/model";

final String DEFAULT_EXCEPTION_API = "generated/com/sngular/multifileplugin/testCreateBasicDTO/model/exception";

final String COMMON_PATH = "openapigenerator/testCreateBasicDTO/";

final String ASSETS_PATH = COMMON_PATH + "assets/";

final List<String> expectedTestApiFile = List.of(
ASSETS_PATH + "TestApi.java"
);

final List<String> expectedTestApiModelFiles = List.of(
ASSETS_PATH + "model/AddressDTO.java",
ASSETS_PATH + "model/TestDTO.java"
);

return (path) -> commonTest(path, expectedTestApiFile, expectedTestApiModelFiles, DEFAULT_TARGET_API, DEFAULT_MODEL_API, Collections.emptyList(), DEFAULT_EXCEPTION_API);
}

static Function<Path, Boolean> validateIssueFaker() {

final String DEFAULT_TARGET_API = "generated/com/sngular/multifileplugin/testissuefaker";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ static Stream<Arguments> fileSpecToProcess() {
OpenApiGeneratorFixtures.validateValidationAnnotationsLombok(SPRING_BOOT_VERSION)),
Arguments.of("testCreateDTO", OpenApiGeneratorFixtures.TEST_CREATE_DTO,
OpenApiGeneratorFixtures.validateCreateDTO()),
Arguments.of("testCreateBasicDTO", OpenApiGeneratorFixtures.TEST_CREATE_BASIC_DTO,
OpenApiGeneratorFixtures.validateCreateBasicDTO()),
Arguments.of("testIssueFaker", OpenApiGeneratorFixtures.TEST_ISSUE_FAKER,
OpenApiGeneratorFixtures.validateIssueFaker()),
Arguments.of("testDateTime", OpenApiGeneratorFixtures.TEST_DATE_TIME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
openapi: "3.0.0"
info:
contact:
name: Os3 Team
email: os3-info@sngular.com
version: 1.0.0
title: Testing example file
license:
name: MIT
description: Testing example file
servers:
- url: http://localhost:8080/v1
tags:
- name: testAdditionalProperties
description: Test service
paths:
/test:
summary: testCreateDTO
get:
summary: testCreateDTO
description: Test File for SCC MultiApi Plugin.
tags:
- test
operationId: testCreateDTO
responses:
'200':
$ref: '#/components/responses/testCreate'
components:
responses:
testCreate:
description: An object with additional properties
content:
application/json:
schema:
$ref: '#/components/schemas/test'
schemas:
test:
type: object
required:
- id
- age
- properties
properties:
id:
$ref: "#/components/schemas/strProperty"
age:
type: number
format: int32
properties:
$ref: "#/components/schemas/strProperty"
address:
type: object
required:
- country
- city
properties:
street:
$ref: "#/components/schemas/strProperty"
country:
$ref: "#/components/schemas/booleanProperty"
city:
$ref: "#/components/schemas/strProperty"
strProperty:
type: string
booleanProperty:
type: boolean
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.sngular.multifileplugin.testCreateBasicDTO;

import java.util.Optional;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.http.MediaType;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import com.sngular.multifileplugin.testCreateBasicDTO.model.TestDTO;

public interface TestApi {

/**
* GET /test: testCreateDTO
* @return An object with additional properties; (status code 200)
*/

@Operation(
operationId = "testCreateDTO",
summary = "testCreateDTO",
tags = {"test"},
responses = {
@ApiResponse(responseCode = "200", description = "An object with additional properties", content = @Content(mediaType = "application/json", schema = @Schema(implementation = TestDTO.class)))
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/test",
produces = {"application/json"}
)

default ResponseEntity<TestDTO> testCreateDTO() {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}

}
Loading

0 comments on commit 8fab074

Please sign in to comment.