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

Java output - Using jakarta.validation.constraints instead of javax.validation.constraints #1807

Open
jmleyman opened this issue Feb 14, 2024 · 3 comments
Labels
enhancement New feature or request Java generator Anything related to the Java generator

Comments

@jmleyman
Copy link

Reason/Context

  • Why we need this improvement?
    With Spring Boot 3, the validation dependencies have migrated to jakarta.validation et the use of javax.validation in projects is discouraged by Spring for compatibility raisons.
  • How will this change help?
    Give the possibility to use jakarta.validation will avoid to have un compatible dependency in one project

Description

  • What changes have to be introduced?
    It will be great if the possibility to have de new Preset to have the right dependancy added like a JAKARTA_CONSTRAINTS_PRESET or have an option to chose which dependency we would add in JAVA_CONSTRAINTS_PRESET ("import javax.validation.constraints.;" (default) or "import jakarta.validation.constraints.;".
  • Will this be a breaking change?
    No breaking change for proposition one is simply an add.
    The second proposition have need to change the type of JAVA_CONSTRAINTS_PRESET to manage options
  • How could it be implemented/designed?

Solution one

Add in the file modelina/src/generators/java/presets/constraitntsPreset.ts the code below.
It's a copy of JAVA_CONSTRAINTS_PRESET with the only change in for the addDependency

export const JAKARTA_CONSTRAINTS_PRESET: JavaPreset = {
    class: {
        self({renderer, content}) {
            renderer.dependencyManager.addDependency(
                'import jakarta.validation.constraints.*;'
            );
            return content;
        },
        // eslint-disable-next-line sonarjs/cognitive-complexity
        property({ renderer, property, content, model }) {
            if (model.options.isExtended) {
                return '';
            }

            const annotations: string[] = [];

            if (property.required) {
                annotations.push(renderer.renderAnnotation('NotNull'));
            }
            const originalInput = property.property.originalInput;

            // string
            if (property.property instanceof ConstrainedStringModel) {
                const pattern = originalInput['pattern'];
                if (pattern !== undefined) {
                    annotations.push(
                        renderer.renderAnnotation('Pattern', {
                            regexp: renderer.renderStringLiteral(pattern)
                        })
                    );
                }
                const minLength = originalInput['minLength'];
                const maxLength = originalInput['maxLength'];
                if (minLength !== undefined || maxLength !== undefined) {
                    annotations.push(
                        renderer.renderAnnotation('Size', {
                            min: minLength,
                            max: maxLength
                        })
                    );
                }
            }

            // number/integer
            if (
                property.property instanceof ConstrainedFloatModel ||
                property.property instanceof ConstrainedIntegerModel
            ) {
                const minimum = originalInput['minimum'];
                if (minimum !== undefined) {
                    annotations.push(renderer.renderAnnotation('Min', minimum));
                }
                const exclusiveMinimum = originalInput['exclusiveMinimum'];
                if (exclusiveMinimum !== undefined) {
                    annotations.push(
                        renderer.renderAnnotation('Min', exclusiveMinimum + 1)
                    );
                }
                const maximum = originalInput['maximum'];
                if (maximum !== undefined) {
                    annotations.push(renderer.renderAnnotation('Max', maximum));
                }
                const exclusiveMaximum = originalInput['exclusiveMaximum'];
                if (exclusiveMaximum !== undefined) {
                    annotations.push(
                        renderer.renderAnnotation('Max', exclusiveMaximum - 1)
                    );
                }
            }

            // array
            if (property.property instanceof ConstrainedArrayModel) {
                const minItems = originalInput['minItems'];
                const maxItems = originalInput['maxItems'];
                if (minItems !== undefined || maxItems !== undefined) {
                    annotations.push(
                        renderer.renderAnnotation('Size', { min: minItems, max: maxItems })
                    );
                }
            }

            return renderer.renderBlock([...annotations, content]);
        }
    }
};

Solution two:

Modify the file modelina/src/generators/java/presets/constraitntsPreset.ts with the code below.

export interface ConstraintPresetOptions {
    useJakarta: boolean
}
export const JAVA_CONSTRAINTS_PRESET: JavaPreset<ConstraintPresetOptions> = {
    class: {
        self({ renderer, model, content, options }) {
            options = options || {};
            if (options.useJakarta === undefined || options.useJakarta === false) {
                renderer.dependencyManager.addDependency(
                    'import javax.validation.constraints.*;'
                );
            } else  {
                renderer.dependencyManager.addDependency(
                    'import jakarta.validation.constraints.*;'
                );
            }

            return content;
        },
        // eslint-disable-next-line sonarjs/cognitive-complexity
        property({ renderer, property, content, model }) {
            [...]
        }
    }
};
@jmleyman jmleyman added the enhancement New feature or request label Feb 14, 2024
Copy link
Contributor

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@jonaslagoni
Copy link
Sponsor Member

Makes sense to use options for this @jmleyman ✌️ Want to provide the PR?

@jonaslagoni jonaslagoni added the Java generator Anything related to the Java generator label Feb 14, 2024
@jmleyman
Copy link
Author

I will try to provide the PR with options soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Java generator Anything related to the Java generator
Projects
None yet
Development

No branches or pull requests

2 participants