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
2 changes: 1 addition & 1 deletion boat-diff/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.backbase.oss</groupId>
<artifactId>backbase-openapi-tools</artifactId>
<version>0.1.0.0</version>
<version>0.1.1-SNAPSHOT</version>
</parent>

<artifactId>boat-diff</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion boat-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.backbase.oss</groupId>
<artifactId>backbase-openapi-tools</artifactId>
<version>0.1.0.0</version>
<version>0.1.1-SNAPSHOT</version>
</parent>
<artifactId>boat-engine</artifactId>
<packaging>jar</packaging>
Expand Down
21 changes: 12 additions & 9 deletions boat-engine/src/main/java/com/backbase/oss/boat/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ public class Exporter {
public static final String NO_DESCRIPTION_AVAILABLE = "No description available";
private static ObjectMapper mapper = Utils.createObjectMapper();

private static boolean convertJsonExamplesToYaml;
private static boolean addJavaTypeExtensions;

private Exporter() {
throw new AssertionError("Private constructor");
}

public static OpenAPI export(File inputFile, boolean convertJsonExamplesToYaml, List<Transformer> transformers)
public static OpenAPI export(File inputFile, boolean addJavaTypeExtensions, List<Transformer> transformers)
throws ExportException {
OpenAPI exported = export(inputFile, convertJsonExamplesToYaml);
OpenAPI exported = export(inputFile, addJavaTypeExtensions);
transformers.forEach(transformer -> transformer.transform(exported, new HashMap()));
return exported;
}


public static OpenAPI export(File inputFile, boolean convertJsonExamplesToYaml) throws ExportException {
public static OpenAPI export(File inputFile, boolean addJavaTypeExtensions) throws ExportException {

// Guess Service Name
AtomicReference<String> serviceName = new AtomicReference<>("serviceName");
Expand All @@ -105,12 +105,11 @@ public static OpenAPI export(File inputFile, boolean convertJsonExamplesToYaml)
.findFirst()
.ifPresent(s -> serviceName.set(s.replaceAll("-spec", "-service")));

return export(serviceName.get(), inputFile, convertJsonExamplesToYaml);
return export(serviceName.get(), inputFile, addJavaTypeExtensions);
}

public static OpenAPI export(String serviceName, File inputFile, boolean convertJsonExamplesToYaml)
public static OpenAPI export(String serviceName, File inputFile, boolean addJavaTypeExtensions)
throws ExportException {
Exporter.convertJsonExamplesToYaml = convertJsonExamplesToYaml;

File parentFile = inputFile.getParentFile();
URL baseUrl;
Expand Down Expand Up @@ -142,9 +141,13 @@ public static OpenAPI export(String serviceName, File inputFile, boolean convert
Components components = new Components();
components.setSchemas(new TreeMap<>(String.CASE_INSENSITIVE_ORDER));

JsonSchemaToOpenApi jsonSchemaToOpenApi = new JsonSchemaToOpenApi(baseUrl, components, ramlTypeReferences,
convertJsonExamplesToYaml);
JsonSchemaToOpenApi jsonSchemaToOpenApi = new JsonSchemaToOpenApi(
baseUrl,
components,
ramlTypeReferences,
addJavaTypeExtensions);

assert ramlApi != null;
Map<String, TypeDeclaration> types = collectTypesFromRamlSpec(ramlApi);
List<Operation> operations = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ class JsonSchemaToOpenApi {
private final URL baseUrl;
private final Components components;
private final Map<String, String> ramlTypeReferences;
private final boolean convertJsonExamplesToYaml;
private final boolean addJavaTypeExtensions;

private ObjectMapper objectMapper = new ObjectMapper();

public JsonSchemaToOpenApi(URL baseUrl, Components components, Map<String, String> ramlTypeReferences, boolean convertJsonExamplesToYaml) {
public JsonSchemaToOpenApi(URL baseUrl, Components components, Map<String, String> ramlTypeReferences, boolean addJavaTypeExtensions) {

this.baseUrl = baseUrl;
this.components = components;
this.jsonSchemas = new TreeMap<>();
this.referenceNames = HashBiMap.create();
this.ramlTypeReferences = ramlTypeReferences;
this.convertJsonExamplesToYaml = convertJsonExamplesToYaml;
this.addJavaTypeExtensions = addJavaTypeExtensions;
}

private Map<String, JsonNode> jsonSchemas;
Expand Down Expand Up @@ -106,7 +107,7 @@ public Schema convert(String name, JSONTypeDeclaration typeDeclaration) throws E
if (schema == null) {
referenceNames.put(baseUrl.toString() + "/" + name + ".raml", name);
schema = createNewSchema(null, jsonSchema, name, components, baseUrl);
schema.setExample(ExampleUtils.getExampleObject(typeDeclaration.example(), convertJsonExamplesToYaml));
schema.setExample(ExampleUtils.getExampleObject(typeDeclaration.example(), true));
components.addSchemas(name, schema);
}
log.debug("Created new schema from: {} as: {}", name, schemaName);
Expand All @@ -118,7 +119,7 @@ public Schema convert(String name, JSONTypeDeclaration typeDeclaration) throws E
throw new ExportException("Cannot dereference json schema from type: " + typeDeclaration.name(), e);
}

schema.setExample(ExampleUtils.getExampleObject(typeDeclaration.example(), convertJsonExamplesToYaml));
schema.setExample(ExampleUtils.getExampleObject(typeDeclaration.example(), true));

return schema;

Expand Down Expand Up @@ -283,11 +284,13 @@ public Schema createNewSchema(Schema parent, JsonNode type, String name, Compone
$ref = getReferenceFromParent($ref, parent);
}
schema.set$ref($ref);
if (type.hasNonNull(JAVA_TYPE)) {
schema.addExtension(X_JAVA_TYPE, type.get(JAVA_TYPE).textValue());
}
if (type.hasNonNull("javaEnumNames")) {
schema.addExtension("x-java-enum-names", getEnum(type, "javaEnumNames"));
if (addJavaTypeExtensions) {
if (type.hasNonNull(JAVA_TYPE)) {
schema.addExtension(X_JAVA_TYPE, type.get(JAVA_TYPE).textValue());
}
if (type.hasNonNull("javaEnumNames")) {
schema.addExtension("x-java-enum-names", getEnum(type, "javaEnumNames"));
}
}
schema.setEnum(getEnum(type));
return schema;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.backbase.oss.boat.transformers;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.ObjectSchema;
import java.util.HashMap;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AdditionalPropertiesAdder implements Transformer {

private final List<String> schemaNames;

public AdditionalPropertiesAdder(List<String> schemaNames) {
this.schemaNames = schemaNames;
}

@Override
public void transform(OpenAPI openAPI, HashMap<String, Object> options) {
openAPI.getComponents().getSchemas().values().stream()
.filter(schema -> schemaNames.contains(schema.getName()))
.forEach(schema -> {
log.info("Adding property: \"additions\" to Schema: {}", schema.getName());
ObjectSchema propertiesItem = new ObjectSchema();
propertiesItem.setAdditionalProperties(true);
schema.addProperties("additions", propertiesItem);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.backbase.oss.boat.transformers;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.License;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LicenseAdder implements Transformer {

public LicenseAdder(String licenseName, String licenseUrl) {
this.licenseName = licenseName;
this.licenseUrl = licenseUrl;
}

private final String licenseName;
private final String licenseUrl;

@Override
public void transform(OpenAPI openAPI, HashMap<String, Object> options) {
if (openAPI.getInfo().getLicense() == null) {
openAPI.getInfo().setLicense(new License().name(licenseName).url(licenseUrl));
log.info("Adding License: {} with url: {} to Schema: {}", licenseName, licenseUrl, openAPI.getInfo().getTitle());
}
}
}
5 changes: 5 additions & 0 deletions boat-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Finds files name `api.raml`, `client-api.raml` or `service-api.raml`.
Processes these files (and the json schemes they refer to) to produce `open-api.yaml` files in the output directory.

Configuration example

```$xml
<build>
<plugins>
Expand All @@ -33,6 +34,10 @@ Configuration example
<description>mock-api-server</description>
</server>
</servers>
<addAdditionalProperties>
<schema>User</schema>
<schema>UserItem</schema>
</addAdditionalProperties>
</configuration>
</plugin>
</plugins>
Expand Down
2 changes: 1 addition & 1 deletion boat-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.backbase.oss</groupId>
<artifactId>backbase-openapi-tools</artifactId>
<version>0.1.0.0</version>
<version>0.1.1-SNAPSHOT</version>
</parent>

<artifactId>boat-maven-plugin</artifactId>
Expand Down
Loading