openapi-processor / openapi-processor-spring

an OpenAPI 3.0 & 3.1 interface & model java code generator for Spring Boot
https://docs.openapiprocessor.io
Apache License 2.0
37 stars 9 forks source link

Annotation-Processing for Classes as Parameters #161

Closed Snap252 closed 1 year ago

Snap252 commented 1 year ago

It's me again ;)

I want to create an annotation (javax.validation.something) with a given Parameter like group that accespts not only numbers / strings but classes / class[]

see: https://openapiprocessor.io/spring/2023.1/mapping/annotation.html

we want something like - type: NachrichtValidationPart @ de.ba.kodi.nachrichten.validation.plausibility.AlreadySent(groups = java.lang.String.class) in mapping.yaml

to get something like: (see groups in first line)

@AlreadySent(groups = java.lang.String.class)
@Generated(
        value = "openapi-processor-spring",
        version = "2023.1.2",
        date = "2023-05-03T10:27:14.553936200+02:00")
public class NachrichtValidationPart {

    @NotNull
    @JsonProperty("nachrichtId")
    private UUID nachrichtId;

with annotation implementation:

package de.ba.kodi.nachrichten.validation.plausibility;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;

@Documented
@Constraint(validatedBy = AlreadySentValidator.class)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AlreadySent {

  String message() default "Zeitpunkt liegt in Zukunft";

  Class<?>[] groups() default {};

  Class<? extends Payload>[] payload() default {};

}

error:

Caused by: io.openapiprocessor.core.processor.mapping.v2.parser.antlr.MappingParserException: mismatched input 'java.lang.String' expecting {Boolean, Identifier, String, Number}
    at io.openapiprocessor.core.processor.mapping.v2.parser.antlr.MappingErrorListener.syntaxError (MappingErrorListener.kt:22)
    at org.antlr.v4.runtime.ProxyErrorListener.syntaxError (ProxyErrorListener.java:41)
    at org.antlr.v4.runtime.Parser.notifyErrorListeners (Parser.java:544)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportInputMismatch (DefaultErrorStrategy.java:327)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportError (DefaultErrorStrategy.java:139)
    at io.openapiprocessor.core.processor.mapping.v2.parser.antlr.MappingParser.annotationParameterNamed (MappingParser.java:811)
    at io.openapiprocessor.core.processor.mapping.v2.parser.antlr.MappingParser.annotationParameters (MappingParser.java:681)
    at io.openapiprocessor.core.processor.mapping.v2.parser.antlr.MappingParser.annotationType (MappingParser.java:577)
    at io.openapiprocessor.core.processor.mapping.v2.parser.antlr.MappingParser.annotate (MappingParser.java:372)
    at io.openapiprocessor.core.processor.mapping.v2.parser.antlr.MappingParser.mapping (MappingParser.java:161)
    at io.openapiprocessor.core.processor.mapping.v2.parser.antlr.ParserKt.parseMapping (Parser.kt:22)
    at io.openapiprocessor.core.processor.mapping.v2.MappingConverter.convertType (MappingConverter.kt:93)

is there a syntax problem (then an example would be nice), or a non-implemented feature ;) ?

Kind regards Christian

hauner commented 1 year ago

Hi,

what it is trying to tell us is that the annotation parameter value is wrong. The parameter should be an explicit value and not a class name. For example you could write

- type: NachrichtValidationPart @ de.ba.kodi.nachrichten.validation.plausibility.AlreadySent(groups = "foo")

The processor does not know what value the parameter should have, so you have to provide it in the mapping.

Snap252 commented 1 year ago

I know ... but javax.validation API (and other APIs) work with classes / class-arrays annotation parameters

see here custom annotation validation see here default javax.validation

So: I'd likely to see this feature/functionality for code generation :)

hauner commented 1 year ago

ah, now i get it 😉

hauner commented 1 year ago

it is probably just a matter of extending the mapping grammer.