Skip to content

Commit

Permalink
feat: added option to differentiate type imports
Browse files Browse the repository at this point in the history
  • Loading branch information
melistik committed Feb 21, 2024
1 parent 2ede17f commit 4c612be
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.rocketbase.commons.openapi;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.rocketbase.commons.openapi.model.ImportGroup;
import io.rocketbase.commons.openapi.model.OpenApiController;
import io.rocketbase.commons.openapi.model.TypescriptApiField;
import io.rocketbase.commons.util.Nulls;
Expand Down Expand Up @@ -109,6 +110,7 @@ public List<TypescriptApiField> getFields() {
for (Parameter p : operation.getParameters()) {
if (!(config.getTypescriptConverter().hasPageableParameter(parameterTypes)
&& Nulls.notNull(config.getPageableParams()).contains(p.getName()))) {

result.add(convert(p));
}
}
Expand Down Expand Up @@ -232,16 +234,22 @@ public List<List<String>> getInvalidateKeysPrepared(String inputPrefix, String r
.collect(Collectors.toList());
}

public Set<String> getImportTypes() {
public List<ImportGroup> getImportTypes() {
Set<String> types = new HashSet<>();
if (genericReturnType != null) {
types.add(genericReturnType);
}
types.addAll(Nulls.notNull(getFields()).stream()
.filter(Objects::nonNull)
.map(TypescriptApiField::getType)
.collect(Collectors.toSet()));
return config.getTypescriptConverter().getImportTypes(types);
.toList());

Map<String, List<String>> packageMap = types.stream()
.collect(Collectors.groupingBy(v -> config.getTypescriptConverter().getImportPackage(v)));

return packageMap.entrySet().stream()
.map(e -> new ImportGroup(e.getKey(), config.getTypescriptConverter().getImportTypes(new HashSet<>(e.getValue()))))
.collect(Collectors.toList());
}

public String getShortReturnType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public interface OpenApiConverter {
*/
Set<String> getImportTypes(Set<String> allTypes);

/**
* in some cases you want to import dependencies from other packages then your code... then you need to implement this method by your-own
*
* @param type raw name of the java-class that is elected to be imported
* @return a path or package to get it from
*/
default String getImportPackage(String type) {
return "../../model";
}

/**
* used to remove package string from full classpath of class
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.rocketbase.commons.openapi.model;

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

import java.io.Serializable;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ImportGroup implements Serializable {

private String name;

private Set<String> types;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

@Data
Expand All @@ -37,14 +34,20 @@ public OpenApiController(String controllerBean, List<OpenApiControllerMethodExtr
this.openApiConverter = openApiConverter;
}

public Set<String> getImportTypes() {
Set<String> result = new HashSet<>();
public Collection<ImportGroup> getImportTypes() {
Map<String, ImportGroup> packMap = new TreeMap<>();
for (OpenApiControllerMethodExtraction m : Nulls.notNull(methods)) {
if (m != null && m.getImportTypes() != null) {
result.addAll(m.getImportTypes());
}
Nulls.notNull(m, OpenApiControllerMethodExtraction::getImportTypes, new ArrayList<ImportGroup>())
.stream()
.forEach(i -> {
if (packMap.containsKey(i.getName())) {
packMap.get(i.getName()).getTypes().addAll(i.getTypes());
} else {
packMap.put(i.getName(), i);
}
});
}
return result;
return packMap.values();
}

protected String toKebabCase(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import { buildRequestorFactory, RequestorBuilder, PageableResult } from "@rocketbase/commons-core";
import { PageableRequest } from "../..";
{% if controller.importTypes is not empty %}
import { {% for i in controller.importTypes %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} } from "../../model";
{% for pack in controller.importTypes %}
import { {% for i in pack.types %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} } from "{{- pack.name -}}";
{% endfor %}
{% endif %}
import { AxiosInstance, AxiosRequestConfig } from "axios";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { PageableResult } from "@rocketbase/commons-core";
import { PageableRequest, useApi } from "../..";
import { createPaginationOptions } from "../util";
{% if controller.importTypes is not empty %}
import { {% for i in controller.importTypes %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} } from "../../model";
{% for pack in controller.importTypes %}
import { {% for i in pack.types %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} } from "{{- pack.name -}}";
{% endfor %}
{% endif %}


Expand Down

0 comments on commit 4c612be

Please sign in to comment.