Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] multipleOf-Property not supported by codegens #2192

Open
SPSpunkt opened this issue Feb 19, 2019 · 2 comments
Open

[BUG] multipleOf-Property not supported by codegens #2192

SPSpunkt opened this issue Feb 19, 2019 · 2 comments

Comments

@SPSpunkt
Copy link

Description

I have an object that uses multipleOf-Property on numbers (same workaround as described in OAI/OpenAPI-Specification#602), but there seems to be no way to use mustache templates for customization, because the current templating process does not respect the multipleOf-Property in anyway.

openapi-generator version

I used OpenAPI generator v3.3.2 +

OpenAPI declaration file content or url
   amount:
      type: number
      multipleOf: 0.01
Suggest a fix

Making multipleOf-Hook available for templating. (helpful e.g. for creating Custom Constraints/Bean Validation in Java)

@wrssmi
Copy link

wrssmi commented Dec 17, 2019

Support of multipleOf in CodegenProperty and CodegenParameter should be implemented. Currently only CodegenParameter has the field multipleOf.

@yasammez
Copy link
Contributor

yasammez commented Jan 16, 2020

To fully resolve this issue, one would need templates which make use of multipleOf. For Java there is the problem that no bean validation annotation provided in javax.validation is applicable. Writing a custom annotation and validator is easy,

@Target({METHOD, FIELD})
@Retention(RUNTIME)
@Repeatable(MultipleOf.List.class)
@Constraint(validatedBy = MultipleOfValidator.class)
public @interface MultipleOf {

    double value();

    String message() default "{error.multipleOf}";

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

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

    @Target({ METHOD, FIELD })
    @Retention(RUNTIME)
    @Documented
    @interface List {
        MultipleOf[] value();
    }

}
public class MultipleOfValidator implements ConstraintValidator<MultipleOf, Number> {

    private double value;

    @Override
    public void initialize(MultipleOf constraintAnnotation) {
        this.value = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Number value, ConstraintValidatorContext context) {
        return (value == null) || (value.doubleValue() / this.value) % 1 == 0;
    }

}

however I do not know where to put these and if the project would be open to templates like this. So my question is: would you accept a PR which provides templates for this custom annotation and validator (and where should I put them?) or shall I put these things into my own codebase and use custom templates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants