Skip to content

Commit

Permalink
feat: add TransferTypes plugin for Generator (#3171)
Browse files Browse the repository at this point in the history
* refactor: add transferTypes functionality

* refactor(generator-plugin-subtypes): small code improvements + update code

* refactor: address code changes

* refactor(generator-plugin-client): revert some changes

* feat(generator-plugin-transfertypes): add transfer type plugin for MultipartFile

* feat(generator-cli): remove custom File import

* chore(generator-plugin-transfertypes): fix dependencies

* test(generator-cli): remove custom File import

* style(parser-jvm-plugin-transfertypes): format

* fix(generator-plugin-subtypes): lint issues

* chore(generator-plugin-transfer): rename dir & fix deps

* chore(generator-plugin-transfer): fix version

* refactor: use d.ts

* refactor(generator-core): add comment
  • Loading branch information
Lodin authored Feb 4, 2025
1 parent 40c4ef3 commit e238c32
Show file tree
Hide file tree
Showing 50 changed files with 754 additions and 221 deletions.
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,10 +26,9 @@
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
Expand All @@ -48,6 +48,8 @@ public final class TransferTypesPlugin
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 Down
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() {
}
1 change: 0 additions & 1 deletion packages/ts/generator-cli/src/GeneratorIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { dirname, isAbsolute, join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import type File from '@vaadin/hilla-generator-core/File.js';
import Plugin, { type PluginConstructor } from '@vaadin/hilla-generator-core/Plugin.js';
import type LoggerFactory from '@vaadin/hilla-generator-utils/LoggerFactory.js';
import GeneratorIOException from './GeneratorIOException.js';
Expand Down
1 change: 0 additions & 1 deletion packages/ts/generator-cli/test/GeneratorIO.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { statSync } from 'node:fs';
import { chmod, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import File from '@vaadin/hilla-generator-core/File.js';
import LoggerFactory from '@vaadin/hilla-generator-utils/LoggerFactory.js';
import chaiAsPromised from 'chai-as-promised';
import { expect, chai, describe, it, beforeEach, afterEach } from 'vitest';
Expand Down
3 changes: 0 additions & 3 deletions packages/ts/generator-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
},
"./SharedStorage.js": {
"types": "./SharedStorage.d.ts"
},
"./utils.js": {
"default": "./utils.js"
}
},
"repository": {
Expand Down
21 changes: 0 additions & 21 deletions packages/ts/generator-core/src/File.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/ts/generator-core/src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ 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';
import type SharedStorage from './SharedStorage.js';
import type { SharedStorage } from './SharedStorage.js';

export type GeneratorContext = Readonly<{
logger: LoggerFactory;
Expand Down Expand Up @@ -37,6 +36,7 @@ export default class Generator {
outputDir: this.#outputDir,
pluginStorage: new Map(),
sources: [],
transferTypes: new Map(),
};

this.#logger.global.debug('Executing plugins');
Expand Down
4 changes: 2 additions & 2 deletions packages/ts/generator-core/src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type LoggerFactory from '@vaadin/hilla-generator-utils/LoggerFactory.js';
import type { Logger } from '@vaadin/hilla-generator-utils/LoggerFactory.js';
import type { Constructor } from 'type-fest';
import type ReferenceResolver from './ReferenceResolver.js';
import type SharedStorage from './SharedStorage.js';
import type { SharedStorage } from './SharedStorage.js';

export default abstract class Plugin {
readonly resolver: ReferenceResolver;
Expand All @@ -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>>;
3 changes: 2 additions & 1 deletion packages/ts/generator-core/src/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import type LoggerFactory from '@vaadin/hilla-generator-utils/LoggerFactory.js';
import type Plugin from './Plugin.js';
import type { PluginConstructor } from './Plugin.js';
import type ReferenceResolver from './ReferenceResolver.js';
import type SharedStorage from './SharedStorage.js';
import type { SharedStorage } from './SharedStorage.js';

export default class PluginManager {
readonly #plugins: Plugin[];

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

0 comments on commit e238c32

Please sign in to comment.