OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][SPRING] missing model and validation on $ref array of strings

Open
#10,026 1 comment 1 reaction 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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

OpenAPI generator drops expected validation when referencing an array type with string items that have constraints.

openapi-generator version
openapi-generator-cli 5.2.0
  commit : 90f7bcd
  built  : 2021-07-09T09:41:46Z
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url

json snippet (Full example: gist)

...
   "components": {
      "schemas": {
         "DogArray": {
            "type": "array",
            "items": {
               "type": "object",
               "properties": {
                  "DogName": {
                     "type": "string",
                     "maxLength": 50,
                     "minLength": 1
                  }
               }
            }
         },
         "Output": {
            "type": "object",
            "properties": {
               "DogsMissingValidation": {
                  "$ref": "#/components/schemas/DogArray"
               },
               "DogsWithValidation": {
                  "type": "array",
                  "items": {
                     "type": "object",
                     "properties": {
                        "DogName": {
                           "type": "string",
                           "maxLength": 50,
                           "minLength": 1
                        }
                     }

Note that "DogsWithValidation"'s schema is a copy and paste of #/components/schemas/DogArray 's schema

Generation Details

"DogsMissingValidation" maps to List<Object>. Where as the unreferenced type, "DogsWithValidation" maps to List<OutputDogsWithValidation>, which has validation in its underlying type. I would expect both of these to be the same, or at the very least both have validation.

Current output
public class Output   {
  @JsonProperty("DogsMissingValidation")
  @Valid
  private List<Object> dogsMissingValidation = null;

  @JsonProperty("DogsWithValidation")
  @Valid
  private List<OutputDogsWithValidation> dogsWithValidation = null;

Full Output.java: gist
Full OutputDogsWithValidation.java: gist

Steps to reproduce

CLI tool via homebrew
openapi-generator generate -i ./dogsapi.json -g spring -o ./tmp/oasOutput/
Seeing same thing with Java generator too.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/4947 -- similar but doesn't address $ref discrepancy.

Suggest a fix

😔

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 by running the supplied OpenAPI declaration through the openapi-generator CLI with the spring generator and compare the generated Output.java and OutputDogsWithValidation.java examples. Trace how the referenced array schema and its string constraints are represented; done means both equivalent array properties retain the expected model and validation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.