Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

303 error using the plugin with the openapi basic configuration from readme #304

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
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.1.0</version>
<version>5.1.1</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class ApiTool {

public static final String RESPONSES = "responses";

public static final String REQUEST_BODIES = "requestBodies";

private static final String PACKAGE_SEPARATOR_STR = ".";

private ApiTool() {
Expand Down Expand Up @@ -127,6 +129,10 @@ public static Map<String, JsonNode> getResponseSchemas(final JsonNode openApi) {
return getComponentSchemasByType(openApi, RESPONSES);
}

public static Map<String, JsonNode> getRequestBodySchemas(final JsonNode openApi) {
return getComponentSchemasByType(openApi, REQUEST_BODIES);
}

private static Map<String, JsonNode> getComponentSchemasByType(final JsonNode openApi, final String schemaType) {
final var schemasMap = new HashMap<String, JsonNode>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ public class GeneratorTemplateException extends RuntimeException {

private static final String ERROR_MESSAGE = "A Template Factory error has been occurred: %s";

private static final String ERROR_MESSAGE_FOR_FILE = "A Template Factory error has been occurred working with the file %s error description: %s";

public GeneratorTemplateException(final String message, final Throwable cause) {
super(String.format(ERROR_MESSAGE, message), cause);
}

public GeneratorTemplateException(final String message, final String filePath, final Throwable cause) {
super(String.format(ERROR_MESSAGE_FOR_FILE, filePath, message), cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private GlobalObject createApiTemplate(final SpecFile specFile, final String fil
try {
templateFactory.fillTemplate(filePathToSave, specFile, javaFileName, pathObjects, authObject);
} catch (IOException | TemplateException e) {
throw new GeneratorTemplateException("Error filling the template", e);
throw new GeneratorTemplateException("Error filling the template", specFile.getFilePath(), e);
}

if (Boolean.TRUE.equals(specFile.isCallMode())) {
Expand Down Expand Up @@ -250,7 +250,6 @@ private String convertPackageToTargetPath(final String fileSpecPackage, final bo
return FilenameUtils.concat(processedGeneratedSourcesFolder, PACKAGE_SEPARATOR.matcher(toMatch).replaceAll("/"));
}

@SuppressWarnings("checkstyle:LambdaBodyLength")
private void processModels(
final SpecFile specFile, final String fileModelToSave, final String modelPackage, final Map<String, JsonNode> basicSchemaMap,
final boolean overwrite) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class GlobalObject {

private Map<String, JsonNode> responseMap;

private Map<String, JsonNode> requestBodyMap;

public Optional<JsonNode> getSchemaNode(final String schemaName) {
return Optional.ofNullable(schemaMap.get(schemaName));
}
Expand All @@ -50,6 +52,10 @@ public Optional<JsonNode> getParameterNode(final String schemaName) {
return Optional.ofNullable(parameterMap.get(schemaName));
}

public Optional<JsonNode> getRequestBodyNode(final String schemaName) {
return Optional.ofNullable(requestBodyMap.get(schemaName));
}

public static class GlobalObjectBuilder {

private final List<String> serverUrl = new ArrayList<>();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static GlobalObject mapOpenApiObjectToOurModels(final JsonNode openAPI, f
globalObject.schemaMap(ApiTool.getComponentSchemas(openAPI));
globalObject.parameterMap(ApiTool.getParameterSchemas(openAPI));
globalObject.responseMap(ApiTool.getResponseSchemas(openAPI));
globalObject.requestBodyMap(ApiTool.getRequestBodySchemas(openAPI));
} else {
globalObject.schemaMap(new HashMap<>());
}
Expand Down Expand Up @@ -208,11 +209,20 @@ private static List<RequestObject> mapRequestObject(
final String operationIdWithCap = operationId.substring(0, 1).toUpperCase() + operationId.substring(1);
if (ApiTool.hasNode(operation, REQUEST_BODY)) {
final var requestBody = ApiTool.getNode(operation, REQUEST_BODY);
requestObjects.add(RequestObject.builder()
.required(ApiTool.hasNode(requestBody, REQUIRED))
.contentObjects(mapContentObject(specFile, ApiTool.getNode(requestBody, CONTENT),
"InlineObject" + operationIdWithCap, globalObject, baseDir))
.build());
if (!ApiTool.hasRef(requestBody)) {
requestObjects.add(RequestObject.builder()
.required(ApiTool.hasNode(requestBody, REQUIRED))
.contentObjects(mapContentObject(specFile, ApiTool.getNode(requestBody, CONTENT),
"InlineObject" + operationIdWithCap, globalObject, baseDir))
.build());
} else {
final var requestBodyNode = globalObject.getRequestBodyNode(MapperUtil.getRefSchemaName(requestBody)).orElseThrow();
requestObjects.add(RequestObject.builder()
.required(ApiTool.hasNode(requestBody, REQUIRED))
.contentObjects(mapContentObject(specFile, ApiTool.getNode(requestBodyNode, CONTENT),
operationIdWithCap, globalObject, baseDir))
.build());
}
}
return requestObjects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private static void processRequestBody(final HashMap<String, JsonNode> basicJson
if (ApiTool.hasNode(operation, "requestBody") && !operation.at("/requestBody/content").isMissingNode()) {
final var content = operation.at("/requestBody/content");
final var schema = content.findValue("schema");
if (ApiTool.hasRef(schema)) {
if (!ApiTool.hasRef(schema)) {
basicJsonNodeMap.put("InlineObject" + StringUtils.capitalize(getOperationId(operation)), schema);
} else if (ApiTool.hasItems(schema)) {
basicJsonNodeMap.put("InlineObject" + StringUtils.capitalize(ApiTool.getNodeAsString(operation, "operationId")), ApiTool.getItems(schema));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@ public final class OpenApiGeneratorFixtures {
.build()
);

static final List<SpecFile> TEST_SIMPLE_BUILD = List.of(
SpecFile
.builder()
.filePath("openapigenerator/testSimpleBuild/api-rest.yaml")
.apiPackage("com.sngular.multifileplugin.testsimplebuild")
.modelPackage("com.sngular.multifileplugin.testsimplebuild.model")
.clientPackage("com.sngular.multifileplugin.testsimplebuild.client")
.modelNameSuffix("DTO")
.useLombokModelAnnotation(true)
.build()
);

final static List<SpecFile> TEST_VALIDATION_ANNOTATIONS = List.of(
SpecFile
.builder()
Expand Down Expand Up @@ -1132,6 +1144,41 @@ static Function<Path, Boolean> validateRestrictionsSchema() {
return (path) -> commonTest(path, expectedTestApiFile, expectedTestApiModelFiles, DEFAULT_TARGET_API, DEFAULT_MODEL_API, expectedExceptionFiles, DEFAULT_EXCEPTION_API);
}

static Function<Path, Boolean> validateSimpleBuild() {

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

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

final String COMMON_PATH = "openapigenerator/testSimpleBuild/";

final String ASSETS_PATH = COMMON_PATH + "assets/";

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

final List<String> expectedTestApiModelFiles = List.of(
ASSETS_PATH + "model/ActivatePolicyDTO.java",
ASSETS_PATH + "model/AddressDTO.java",
ASSETS_PATH + "model/CompanyDTO.java",
ASSETS_PATH + "model/ContactDTO.java",
ASSETS_PATH + "model/CreditLimitDTO.java",
ASSETS_PATH + "model/DocumentDTO.java",
ASSETS_PATH + "model/ErrorResponseDTO.java",
ASSETS_PATH + "model/InsuredCreditObjectDTO.java",
ASSETS_PATH + "model/PolicyActivationDTO.java",
ASSETS_PATH + "model/PolicyClaimDTO.java",
ASSETS_PATH + "model/PolicySettlementDTO.java",
ASSETS_PATH + "model/QuoteRequestDTO.java",
ASSETS_PATH + "model/QuoteResponseDTO.java",
ASSETS_PATH + "model/QuoteUpdateResponseDTO.java",
ASSETS_PATH + "model/UpdateQuoteDTO.java"
);

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

static Function<Path, Boolean> validateValidationAnnotations(int springBootVersion) {

final String DEFAULT_TARGET_API = "generated/com/sngular/multifileplugin/testapi";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ static Stream<Arguments> fileSpecToProcess() {
Arguments.of("testApiWithNoComponents", OpenApiGeneratorFixtures.TEST_API_WITH_NO_COMPONENTS,
OpenApiGeneratorFixtures.validateApiWithNoComponents()),
Arguments.of("testRestrictionSchema", OpenApiGeneratorFixtures.TEST_RESTRICTION_SCHEMA,
OpenApiGeneratorFixtures.validateRestrictionsSchema())
OpenApiGeneratorFixtures.validateRestrictionsSchema()),
Arguments.of("testSimpleBuild", OpenApiGeneratorFixtures.TEST_SIMPLE_BUILD,
OpenApiGeneratorFixtures.validateSimpleBuild())
);
}

Expand Down
Loading
Loading