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 @@ -20,6 +20,7 @@
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenResponse;
import org.openapitools.codegen.utils.ModelUtils;

import java.io.IOException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -65,9 +66,16 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
}

if (openAPI.getComponents().getExamples() != null) {
Set<String> imports = new HashSet<>();
additionalProperties.put("examples", openAPI.getComponents().getExamples().entrySet().stream()
.map(exampleEntry -> mapComponentExample(imports, exampleEntry))
.map(this::mapComponentExample)
.collect(Collectors.toList()));
}

// We need freeForm objects as models as well, since they are being referenced with $ref
if (openAPI.getComponents().getSchemas() != null) {
additionalProperties.put("freeFormModels", openAPI.getComponents().getSchemas().entrySet().stream()
.filter(freeFormModel -> ModelUtils.isFreeFormObject(freeFormModel.getValue()))
.map(this::mapFreeFormObject)
.collect(Collectors.toList()));
}

Expand All @@ -90,12 +98,17 @@ private CodegenParameter mapComponentRequestBody(Set<String> imports, java.util.
return fromRequestBody(requestBody, imports, name);
}

private BoatExample mapComponentExample(Set<String> imports, Map.Entry<String, Example> namedExample) {
private BoatExample mapComponentExample( Map.Entry<String, Example> namedExample) {
String key = namedExample.getKey();
Example example = namedExample.getValue();
return new BoatExample(key,"", example , false);
}

private Object mapFreeFormObject(Map.Entry<String, Schema> freeFormObject) {//NOSONAR
String name = freeFormObject.getKey();
return fromModel(name, freeFormObject.getValue());
}

private CodegenParameter mapComponentParameter(Set<String> imports, java.util.Map.Entry<String, Parameter> nameParameter) {
Parameter parameter = nameParameter.getValue();
return fromParameter(parameter, imports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ const data = {
data["parameters"]["{{paramName}}"] = {{{jsonSchema}}};
{{/each}}

{{#each freeFormModels}}
data.models["{{name}}"] = {{{json .}}};
{{/each}}

{{#each swagger.vendorExtensions.x-shared-errors}}
let err = {{{json .}}};
data["errors"][err.errorID] = err;
{{/each}}



module.exports = { data };
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.backbase.oss.boat.loader.OpenAPILoader;
import com.backbase.oss.boat.loader.OpenAPILoaderException;
import com.backbase.oss.codegen.doc.BoatDocsGenerator;
import io.swagger.v3.oas.models.OpenAPI;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -42,6 +41,11 @@ void testGenerateDocs() throws IOException {
File index = new File("target/marina-docs/api.js");
String generated = String.join(" ", Files.readAllLines(Paths.get(index.getPath())));
assertTrue(generated.startsWith("/**"));
assertTrue(generated.contains("Simple API overview"));
assertTrue(generated.contains("appDescription: \"No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)\","));
assertTrue(generated.contains("data.examples[\"BadRequestError\"]"));
assertTrue(generated.contains("data.models[\"ParameterMap\"]"));

}


Expand All @@ -53,7 +57,7 @@ protected File getFile(String name) {
}


private static void generateDocs(File spec) {
private static void generateDocs(File spec) throws IOException {
log.info("Generate docs for: {}", spec);
OpenAPI openAPI = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,38 @@ openapi: "3.0.0"
info:
title: Simple API overview
version: 2.0.0
components:
schemas:
ParameterMap:
type: object
additionalProperties:
type: object
example:
enable_payments: true
examples:
BadRequestError:
summary: BadRequestError
value:
message: Bad Request
errors:
- message: "Value Exceeded. Must be between {min} and {max}."
key: common.api.shoesize
context:
max: "50"
min: "1"
paths:
/foo:
description: Some random description
get:
operationId: foo
summary: Gets a project's parameters.
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ParameterMap'
/:
get:
operationId: listVersionsv2
Expand All @@ -29,6 +60,8 @@ paths:
links:
- href: http://127.0.0.1:8774/v3/
rel: self
bar:
$ref: '#/components/examples/BadRequestError'

'300':
description: |-
Expand Down