Skip to content

Commit

Permalink
fix: generator use only public getters and setters (#2783)
Browse files Browse the repository at this point in the history
* Use only public getters and setters in generator

Fixes: #2587

* Use only public getters and setters in generator

Fixes: #2587

* Add integration test

---------

Co-authored-by: Anton Platonov <[email protected]>
  • Loading branch information
krissvaa and platosha authored Oct 9, 2024
1 parent aec4925 commit edf8c7e
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.vaadin.hilla.parser.plugins.backbone;

import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationConfig;
Expand All @@ -28,7 +29,12 @@
public final class PropertyPlugin
extends AbstractPlugin<BackbonePluginConfiguration> {
private SerializationConfig serializationConfig = new JacksonObjectMapperFactory.Json()
.build().getSerializationConfig();
.build()
.setVisibility(PropertyAccessor.SETTER,
JsonAutoDetect.Visibility.PUBLIC_ONLY)
.setVisibility(PropertyAccessor.GETTER,
JsonAutoDetect.Visibility.PUBLIC_ONLY)
.getSerializationConfig();

@Override
public void enter(NodePath<?> nodePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ public Short getShortWrapper() {
public String getString() {
return "test";
};

protected String getProtectedValue() {
return "protected";
}

protected void setProtectedValue(String protectedValue) {
// ignore
}
}
2 changes: 2 additions & 0 deletions packages/java/tests/spring/endpoints/frontend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Router } from '@vaadin/router';
import './src/test-component.js';
import './src/test-flux.js';
import './src/test-type-script.js';
import './src/test-access-mod.js';

const routes = [
{ path: 'flux', component: 'test-flux' },
{ path: 'type-script', component: 'test-type-script' },
{ path: '', component: 'test-component' },
{ path: 'login', component: 'test-component' },
{ path: 'access-mod', component: 'test-access-mod' },
];

// Vaadin router needs an outlet in the index.html page to display views
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { html, LitElement } from 'lit';
import { customElement, query } from 'lit/decorators.js';
import { AccessModifierEndpoint } from 'Frontend/generated/endpoints';

@customElement('test-access-mod')
export class TestAccessModifierComponent extends LitElement {
@query('#content')
private content!: HTMLOutputElement;
@query('#methods')
private methods!: HTMLOutputElement;

render() {
return html`
<button id="getEntity" @click="${this.getEntity}">Get entity</button>
<output id="content"></output>
<object id="methods"></object>
`;
}

public async getEntity() {
const entity = await AccessModifierEndpoint.getEntity();
if (entity === undefined) {
throw new Error('Missing entity object result from endpoint');
}

this.content.textContent = JSON.stringify(entity);
this.methods.textContent = Object.keys(entity).join(', ');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.vaadin.flow.connect;

import com.vaadin.flow.server.auth.AnonymousAllowed;
import com.vaadin.hilla.Endpoint;
import jakarta.annotation.security.PermitAll;

/**
* Simple Vaadin Connect Service definition.
*/
@Endpoint
@AnonymousAllowed
class AccessModifierEndpoint {

@PermitAll
public ObjectWithDifferentAccessMethods getEntity() {
return new ObjectWithDifferentAccessMethods("private", "protected",
"public", "package-private", "public-getter", "public-setter");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.vaadin.flow.connect;

public class ObjectWithDifferentAccessMethods {
private String privateProp;
private String protectedProp;
private String publicProp;
private String packagePrivateProp;
private String publicGetterProp;
private String publicSetterProp;

public ObjectWithDifferentAccessMethods(String privateProp,
String protectedProp, String publicProp, String packagePrivateProp,
String publicGetterProp, String publicSetterProp) {
this.privateProp = privateProp;
this.protectedProp = protectedProp;
this.publicProp = publicProp;
this.packagePrivateProp = packagePrivateProp;
this.publicGetterProp = publicGetterProp;
this.publicSetterProp = publicSetterProp;
}

private String getPrivateProp() {
return privateProp;
}

private void setPrivateProp(String privateProp) {
this.privateProp = privateProp;
}

protected String getProtectedProp() {
return protectedProp;
}

protected void setProtectedProp(String protectedProp) {
this.protectedProp = protectedProp;
}

public String getPublicProp() {
return publicProp;
}

public void setPublicProp(String publicProp) {
this.publicProp = publicProp;
}

String getPackagePrivateProp() {
return packagePrivateProp;
}

void setPackagePrivateProp(String packagePrivateProp) {
this.packagePrivateProp = packagePrivateProp;
}

public String getPublicGetterProp() {
return publicGetterProp;
}

private void setPublicGetterProp(String publicGetterProp) {
this.publicGetterProp = publicGetterProp;
}

private String getPublicSetterProp() {
return publicSetterProp;
}

public void setPublicSetterProp(String publicSetterProp) {
this.publicSetterProp = publicSetterProp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.connect;

import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;
import org.checkerframework.checker.units.qual.A;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;

/**
* Class for testing issues in a spring-boot container.
*/
public class AccessModifierIT extends ChromeBrowserTest {

private void openTestUrl(String url) {
getDriver().get(getRootURL() + url);
}

private TestBenchElement testAccessMod;
private TestBenchElement methods;

@Override
@Before
public void setup() throws Exception {
super.setup();
open();
TestBenchElement testComponent = $("test-component").waitForFirst();
if (testComponent != null) {
testComponent.$(TestBenchElement.class).id("username")
.sendKeys("user");
testComponent.$(TestBenchElement.class).id("password")
.sendKeys("user");
testComponent.$(TestBenchElement.class).id("login").click();
open();
}
testAccessMod = $("test-access-mod").waitForFirst();
methods = testAccessMod.$(TestBenchElement.class).id("methods");
}

@Override
protected void open() {
openTestUrl("/access-mod");
}

@Test
public void getEntity() {
String endpoint = "getEntity";
exec(endpoint);
String actualText = waitUntil(driver -> methods.getText(), 25);
Assert.assertNotNull(actualText);
Assert.assertTrue(actualText.contains("publicProp"));
Assert.assertFalse(actualText.contains("protectedProp"));
Assert.assertFalse(actualText.contains("packagePrivateProp"));
Assert.assertFalse(actualText.contains("privateProp"));
Assert.assertTrue(actualText.contains("publicGetterProp"));
// Assert.assertTrue(actualText.contains("publicSetterProp")); //
// property is public, but not present in prototype
}

private void exec(String id) {
methods.setProperty("innerText", "");
WebElement button = testAccessMod.$(TestBenchElement.class).id(id);
button.click();
}
}

0 comments on commit edf8c7e

Please sign in to comment.