-
-
Notifications
You must be signed in to change notification settings - Fork 380
Description
Describe the bug
Impossible to do cascade validation when DTOs are generated in Java because nested objects don't have the '@Valid' annotation needed. The DTOs are created using a typescript script with modelina. The preset that handles validation 'JAVA_CONSTRAINTS_PRESET' is correctly called (other validations are correctly generated). We use the version 2.6.0 of the asyncapi specification.
The following snippet in the modelina source code shows how it's supposed to work :
// needs cascade validation if ( property.property instanceof ConstrainedReferenceModel || property.property instanceof ConstrainedObjectModel || property.property instanceof ConstrainedArrayModel ) { annotations.push(renderer.renderAnnotation('Valid')); }
I then assume that our nested objets are not considered as ConstrainedReferenceModel nor ConstrainedObjectModel.
To Reproduce
Steps to reproduce the behavior:
- Use the following snippet in the asyncapi file
parentObject: type: object additionalProperties: false required: - prop1 properties: prop1: { type: string } metadata: $ref: '#/components/schemas/nestedObject' nestedObject: type: object additionalProperties: false required: [ reference ] properties: reference: { type: string } - Generates DTOs with a typical modelina code generator
our ts script looks like this :
const generator = new JavaFileGenerator({ constraints: { enumKey: javaDefaultEnumKeyConstraints({ NAMING_FORMATTER: FormatHelpers.toConstantCase }), propertyKey: javaDefaultPropertyKeyConstraints({ NAMING_FORMATTER: FormatHelpers.toCamelCase }), modelName: javaDefaultModelNameConstraints({ NAMING_FORMATTER: FormatHelpers.toPascalCase }) }, typeMapping: { String: ({dependencyManager, constrainedModel, options, partOfProperty}) => { if (constrainedModel.options.format === "date-time") { dependencyManager.addDependency("import java.time.LocalDateTime;"); return "LocalDateTime"; } if (constrainedModel.options.format === "date") { dependencyManager.addDependency("import java.time.LocalDate;"); return "LocalDate"; } return "String"; } }, indentation: { type: IndentationTypes.SPACES }, collectionType: "List", presets: [ JAVA_JACKSON_PRESET, JAVA_DESCRIPTION_PRESET, { preset: JAVA_CONSTRAINTS_PRESET, options: { useJakarta: true } }, { preset: JAVA_COMMON_PRESET, options: { equal: true, hashCode: true, classToString: true, marshalling: false } }, ] });
`function generate(yamlFilePath: string, srcDirectory: string, packageName: string): any {
const YAML_DIRECTORY = path.dirname(yamlFilePath);
const YAML_FILENAME = path.basename(yamlFilePath);
const TMP_JSON_PATH = path.join(YAML_DIRECTORY, YAML_FILENAME.replace(".yaml", ".json").replace(".yml", ".json"));
const JSON_CONTENT = JSON.stringify(yamlFileToJson(yamlFilePath), null, 2);
fs.writeFileSync(TMP_JSON_PATH, JSON_CONTENT);
const OUTPUT_PATH = "${srcDirectory}/${packageName.replace(/./g, "/")}";
generator.generateToFiles(require(TMP_JSON_PATH), OUTPUT_PATH, {
packageName: packageName,
}, true)
.then(generationSuccess)
.then(a => fs.unlinkSync(TMP_JSON_PATH))
.catch(reason => console.log(ColoredText.red("Generation error: %s"), reason));
}`
function generationSuccess(outputModels: OutputModel[]): void { if (outputModels.length == 0) { console.error(ColoredText.red("Generation error? No file generated !?!")); } else { console.log(ColoredText.GREEN); console.log("Generation successful :"); outputModels.forEach(outputModel => console.log(" " + outputModel.result.match(/package\s+([\w.]+)/)?.[1] + "." + outputModel.modelName + ".java") ); console.log(ColoredText.RESET); } }
Expected behavior
I expect the '@Valid' to be present when generating the DTOs.
Maybe it's not a bug on your side but rather on modelina's side but I'd like to check here first.
Thank you in advance