OpenAPITools / OpenAPITools/openapi-generator

[BUG] [kotlin-spring] Missing validation on collection of models

Open
#5,769 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

We have hand-coded our models, and are now moving over to schema-first so that we can both be certain that our schemas are correct, and ease development by not having to implement everything ourselves.

However, one of our validation requirements don't seem to be included when using the kotlin-spring generator, while it is with the normal java spring generator.
Specifically the validation we're missing is that the EmailAddresses used in an Email field is valid.
In other words, we can validate that the to: List<EmailAddress> field has at least one entry, but we can't validate that to[0].email actually is validated with minlength: 5. We've also used a regex pattern on the EmailAddress.email field with no success.

openapi-generator version

Version 4.2.2. I don't think it's a regression, but I haven't tested any previous versions.

OpenAPI declaration file content or url

https://gist.github.com/tofagerl/f9c315662e2ae2d22b5a75ba803c1171

Command line used for generation

Using the gradle plugin:

task generateEmailSpec(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    validateSpec = true
    inputSpec = "src/main/openapi/email.json"
    generatorName = "kotlin-spring"
    outputDir = "$generatedSources/email"
    apiPackage = "org.entur.email.api"
    modelPackage = "org.entur.email.models"
    configOptions = [
            enumPropertyNaming: 'UPPERCASE',
            interfaceOnly     : 'true',
            gradleBuildFile   : 'false',
            title             : 'Email',
            useBeanValidation: 'true',
            performBeanValidation: 'true'
    ]
}
Steps to reproduce

Run task generateEmailSpec to generate the models, see that there is no @Valid annotation on the field Email.to
Change generatorName to 'spring', and run the task again. Note that the field is now marked as @Valid.

Related issues/PRs

I couldn't find anything

Suggest a fix

I haven't looked much at the codebase, so I don't know how, but it seems like adding the annotation should be possible.

Expected output
        @get:NotNull @get:Size(min=1) 
		@Valid
        @JsonProperty("to") val to: kotlin.collections.List<EmailAddress>,
Actual output
        @get:NotNull @get:Size(min=1) 
        @JsonProperty("to") val to: kotlin.collections.List<EmailAddress>,

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Gradle generateEmailSpec task in the issue and reproduce the kotlin-spring output using the linked OpenAPI declaration. Compare the generated Email.to model field with the spring generator output, then verify that the collection carries @Valid so nested EmailAddress.email constraints are applied.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.