bval-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Romain Manni-Bucau (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (BVAL-167) ConstraintDeclarationException: Illegal strengthening: overridden [constraints] in inheritance hierarchy
Date Sun, 04 Nov 2018 19:19:00 GMT

    [ https://issues.apache.org/jira/browse/BVAL-167?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16674438#comment-16674438
] 

Romain Manni-Bucau edited comment on BVAL-167 at 11/4/18 7:18 PM:
------------------------------------------------------------------

Hi  [~ilgrosso],

This test reproduces it:

{code}
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.bval.jsr;

import java.lang.reflect.Method;

import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;
import javax.validation.executable.ExecutableValidator;

import org.junit.Test;

public class InheritanceTest {
    @Test
    public void validateParams() throws NoSuchMethodException {
        final Api service = new Impl();
        try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
            final ExecutableValidator validator = factory.getValidator().forExecutables();
            final Method method = Api.class.getMethod("read", String.class);
            validator.validateParameters(service, method, new Object[]{""});
        }
    }


    public interface Api {
        String read(@NotNull String key);
    }

    public static abstract class Base {
        public String read(final String key) {
            return null;
        }
    }

    public static class Impl extends Base implements Api {
        @Override
        public String read(final String key) {
            return super.read(key);
        }
    }
}
{code}

The issue is that the hierarchy is broken so the error is accurate and not accurate at the
same time. Since the abstract class does not implement the constract (interface) then the
child class inheriting from the abstract class implementation does not see it as a contract
implementation.

Indeed we can detect that the abstract class implements by signature (~reflection) the contract
but it still violates the java rules bean validation tries to enforce.

[~mbenson] can you review that analysis and maybe org.apache.bval.jsr.metadata.Liskov.StrengtheningIssue
which ignores all these potential rules. I think wen can enhance our impl since it is not
uncommon and I don't see why we would stick to very rude rules even if spec-ed if the TCK
don't enforce them?

In the same time, [~ilgrosso] you can fix it applying the same constraint on the whole hierarchy
or doing a real implementation (maybe using composition pattern instead of a "fake" inheritance
for the technical code?


was (Author: romain.manni-bucau):
Hi  [~ilgrosso], can you try to reproduce it in a unit test?

{code}
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.bval.jsr;

import static java.util.Collections.emptySet;

import java.lang.reflect.Method;
import java.util.Set;

import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;

import org.junit.Test;

public class InheritanceTest {
    @Test
    public void validate() throws NoSuchMethodException {
        final AnyService<TO> service = new AnyServiceImpl();
        try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
            final ExecutableValidator validator = factory.getValidator().forExecutables();
            final Method method = service.getClass().getMethod("read", String.class, SchemaType.class);
            validator.validateParameters(service, method, new Object[]{null, null});
            validator.validateReturnValue(service, method, emptySet());
        }
    }

    public static abstract class AnyTO {
    }

    public static class TO extends AnyTO {
    }

    public static class AttrTO {
    }

    public static class SchemaType {
    }

    public interface JAXRSService {
    }

    public interface AnyService<TO extends AnyTO> extends JAXRSService {
        Set<AttrTO> read(String key, SchemaType schemaType);
    }

    public static abstract class AbstractAnyService<TO extends AnyTO> implements AnyService<TO>
{
        @Override
        public Set<AttrTO> read(final String key, final SchemaType schemaType) {
            return emptySet();
        }
    }

    public static class AnyServiceImpl extends AbstractAnyService<TO> {
    }
}

{code}

does not reproduce it

edit: the error is about read(String,SchemaType,String) and not the method you pointed out,
it also seems to imply some constraint (like @NotNull) was added on the abstract class and
not the interface, can it be a dirty target/ state?

> ConstraintDeclarationException: Illegal strengthening: overridden [constraints] in inheritance
hierarchy
> --------------------------------------------------------------------------------------------------------
>
>                 Key: BVAL-167
>                 URL: https://issues.apache.org/jira/browse/BVAL-167
>             Project: BVal
>          Issue Type: Bug
>    Affects Versions: 2.0.0
>            Reporter: Francesco Chicchiriccò
>            Priority: Major
>             Fix For: 2.0.1
>
>
> I am trying to upgrade BVal to 2.0.0 in Syncope.
> I have changed my POMs as follows:
> * javax.validation:validation-api from 1.1.0.Final to 2.0.1.Final
> * org.apache.bval:bval-jsr from 1.1.2 to 2.0.0
> When I try to reach up some of the REST services (based on CXF 3.2), an [exception|https://paste.apache.org/hxvY]
is thrown; I have read about such exception, but it seems to me that the mentioned methods
in [interface|https://github.com/apache/syncope/blob/2_1_X/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AnyService.java#L70]
and [implementation|https://github.com/apache/syncope/blob/2_1_X/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AbstractAnyService.java#L68]
should comply with hierarchy rules.
> Nevertheless, if I swap
> org.apache.bval:bval-jsr:2.0.0
> with
> org.hibernate.validator:hibernate-validator:6.0.13.Final
> all is working fine.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message