Skip to content

Commit

Permalink
feat(generator-plugin-transfertypes): add transfer type plugin for Mu…
Browse files Browse the repository at this point in the history
…ltipartFile
  • Loading branch information
Lodin committed Jan 22, 2025
1 parent 46c35ef commit 6bf0985
Show file tree
Hide file tree
Showing 36 changed files with 7,613 additions and 8,119 deletions.
14,943 changes: 6,947 additions & 7,996 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@
"license": "Apache-2.0",
"overrides": {
"esbuild": "$esbuild",
"prettier": "$prettier"
"prettier": "$prettier",
"react": "18 || 19",
"chai": "5"
},
"devDependencies": {
"@nx/js": "^20.3.1",
"@preact/signals-react-transform": "^0.5.1",
"@remcovaes/web-test-runner-vite-plugin": "^1.2.2",
"@types/karma": "^6.3.9",
"@types/node": "^22.10.5",
"@vaadin/react-components": "24.7.0-alpha5",
"@vaadin/react-components": "24.7.0-alpha6",
"@vitejs/plugin-react": "^4.3.4",
"@web/test-runner": "^0.19.0",
"chai-dom": "^1.12.0",
Expand Down
131 changes: 68 additions & 63 deletions packages/java/parser-jvm-plugin-transfertypes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,76 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.vaadin</groupId>
<artifactId>hilla-project</artifactId>
<version>24.7-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<parent>
<groupId>com.vaadin</groupId>
<artifactId>hilla-project</artifactId>
<version>24.7-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>hilla-parser-jvm-plugin-transfertypes</artifactId>
<name>Hilla JVM Parser Transfer Types Plugin</name>
<packaging>jar</packaging>
<artifactId>hilla-parser-jvm-plugin-transfertypes</artifactId>
<name>Hilla JVM Parser Transfer Types Plugin</name>
<packaging>jar</packaging>

<properties>
<formatter.basedir>${project.parent.basedir}</formatter.basedir>
</properties>
<properties>
<formatter.basedir>${project.parent.basedir}</formatter.basedir>
</properties>

<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-plugin-backbone</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-runtime-plugin-transfertypes</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-test-utils</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-plugin-backbone</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-runtime-plugin-transfertypes</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla-parser-jvm-test-utils</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import com.vaadin.hilla.mappedtypes.Order;
import com.vaadin.hilla.mappedtypes.Pageable;
import com.vaadin.hilla.mappedtypes.Sort;
Expand All @@ -25,29 +26,29 @@
import com.vaadin.hilla.parser.models.ClassRefSignatureModel;
import com.vaadin.hilla.parser.models.SignatureModel;
import com.vaadin.hilla.parser.plugins.backbone.BackbonePlugin;
import com.vaadin.hilla.parser.plugins.backbone.nodes.CompositeTypeSignatureNode;
import com.vaadin.hilla.parser.plugins.backbone.nodes.TypeSignatureNode;
import com.vaadin.hilla.parser.plugins.backbone.nodes.TypedNode;
import com.vaadin.hilla.runtime.transfertypes.EndpointSubscription;
import com.vaadin.hilla.runtime.transfertypes.File;
import com.vaadin.hilla.runtime.transfertypes.Flux;

public final class TransferTypesPlugin
extends AbstractPlugin<PluginConfiguration> {
extends AbstractPlugin<PluginConfiguration> {
static private final Map<String, Class<?>> classMap = new HashMap<>();

static {
classMap.put("org.springframework.data.domain.Page", List.class);
classMap.put("org.springframework.data.domain.Pageable",
Pageable.class);
Pageable.class);
classMap.put("org.springframework.data.domain.Sort$Order", Order.class);
classMap.put("org.springframework.data.domain.Sort", Sort.class);
classMap.put(UUID.class.getName(), String.class);
classMap.put("reactor.core.publisher.Flux", Flux.class);
classMap.put("com.vaadin.hilla.EndpointSubscription",
EndpointSubscription.class);
EndpointSubscription.class);
classMap.put(JsonNode.class.getName(), Object.class);
classMap.put(ObjectNode.class.getName(), Object.class);
classMap.put(ArrayNode.class.getName(), List.class);
classMap.put("org.springframework.web.multipart.MultipartFile", File.class);
}

public TransferTypesPlugin() {
Expand All @@ -71,7 +72,7 @@ public Collection<Class<? extends Plugin>> getRequiredPlugins() {
@Override
public NodeDependencies scan(@NonNull NodeDependencies nodeDependencies) {
return nodeDependencies.processChildNodes(this::processNodes)
.processRelatedNodes(this::processNodes);
.processRelatedNodes(this::processNodes);
}

private Node<?, ?> mapClassRefNodes(Node<?, ?> node) {
Expand Down Expand Up @@ -99,6 +100,6 @@ private SignatureModel processType(SignatureModel signature) {

var mappedClassInfo = ClassInfoModel.of(classMap.get(className));
return ClassRefSignatureModel.of(mappedClassInfo,
classRef.getTypeArguments(), classRef.getAnnotations());
classRef.getTypeArguments(), classRef.getAnnotations());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.vaadin.hilla.parser.plugins.transfertypes.file;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Endpoint {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vaadin.hilla.parser.plugins.transfertypes.file;

import org.springframework.web.multipart.MultipartFile;

@Endpoint
public class MultipartFileEndpoint {
public void uploadFile(MultipartFile file) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vaadin.hilla.parser.plugins.transfertypes.file;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.Test;

import com.vaadin.hilla.parser.core.Parser;
import com.vaadin.hilla.parser.plugins.backbone.BackbonePlugin;
import com.vaadin.hilla.parser.plugins.transfertypes.TransferTypesPlugin;
import com.vaadin.hilla.parser.plugins.transfertypes.test.helpers.TestHelper;

public class MultipartFileTest {
private final TestHelper helper = new TestHelper(getClass());

@Test
public void should_ReplaceMultipartFileClassWithLocalFileClass()
throws IOException, URISyntaxException {
var openAPI = new Parser()
.classPath(Set.of(helper.getTargetDir().toString()))
.endpointAnnotations(List.of(Endpoint.class))
.addPlugin(new BackbonePlugin())
.addPlugin(new TransferTypesPlugin())
.execute(List.of(MultipartFileEndpoint.class));

helper.executeParserWithConfig(openAPI);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"openapi" : "3.0.1",
"info" : {
"title" : "Hilla Application",
"version" : "1.0.0"
},
"servers" : [
{
"url" : "http://localhost:8080/connect",
"description" : "Hilla Backend"
}
],
"tags" : [
{
"name" : "MultipartFileEndpoint",
"x-class-name" : "com.vaadin.hilla.parser.plugins.transfertypes.file.MultipartFileEndpoint"
}
],
"paths" : {
"/MultipartFileEndpoint/uploadFile" : {
"post" : {
"tags" : [
"MultipartFileEndpoint"
],
"operationId" : "MultipartFileEndpoint_uploadFile_POST",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"type" : "object",
"properties" : {
"file" : {
"nullable" : true,
"anyOf" : [
{
"$ref" : "#/components/schemas/com.vaadin.hilla.runtime.transfertypes.File"
}
]
}
}
}
}
}
},
"responses" : {
"200" : {
"description" : ""
}
}
}
}
},
"components" : {
"schemas" : {
"com.vaadin.hilla.runtime.transfertypes.File" : {
"type" : "object"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.vaadin.hilla.runtime.transfertypes;

public record File() {
}
21 changes: 0 additions & 21 deletions packages/ts/generator-core/src/File.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/ts/generator-core/src/Generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import SwaggerParser from '@apidevtools/swagger-parser';
import type LoggerFactory from '@vaadin/hilla-generator-utils/LoggerFactory.js';
import type { OpenAPIV3 } from 'openapi-types';
import type { ReadonlyDeep } from 'type-fest';
import ts from 'typescript';
import File from './File.js';
import type { PluginConstructor } from './Plugin.js';
import PluginManager from './PluginManager.js';
import ReferenceResolver from './ReferenceResolver.js';
Expand All @@ -29,7 +27,7 @@ export default class Generator {

async process(input: string): Promise<readonly File[]> {
this.#logger.global.debug('Processing OpenAPI');
const api = (await this.#parser.bundle(JSON.parse(input))) as ReadonlyDeep<OpenAPIV3.Document>;
const api = (await this.#parser.bundle(JSON.parse(input))) as OpenAPIV3.Document;

const storage: SharedStorage = {
api,
Expand Down
2 changes: 1 addition & 1 deletion packages/ts/generator-core/src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default abstract class Plugin {

abstract get path(): string;

abstract execute(storage: SharedStorage): Promise<void>;
abstract execute(storage: SharedStorage): Promise<void> | void;
}

export type PluginConstructor = Constructor<Plugin, ConstructorParameters<typeof Plugin>>;
1 change: 1 addition & 0 deletions packages/ts/generator-core/src/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class PluginManager {

constructor(plugins: readonly PluginConstructor[], resolver: ReferenceResolver, logger: LoggerFactory) {
const standardPlugins = [
'TransferTypesPlugin',
'BackbonePlugin',
'ClientPlugin',
'BarrelPlugin',
Expand Down
Loading

0 comments on commit 6bf0985

Please sign in to comment.