Openapi-generator: [BUG][Java] Add support for validation of strings in arrays

Created on 8 Jan 2020  路  2Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

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

When having an object nested in an array's items, like:

type: array
items:
  type: object
  properties:
    item:
      type: string
      pattern: ^abc$

Then validations will be carried correctly through @Valid and other Java bean annotations.

public class InlineObject {
  public static final String SERIALIZED_NAME_ITEMS = "items";
  @SerializedName(SERIALIZED_NAME_ITEMS)
  private List<ApiMyPathItems> items = null;

  // cut for clarity
  @javax.annotation.Nullable
  @Valid
  @ApiModelProperty(value = "")
  public List<ApiMyPathItems> getItems() {
    return items;
  }
  // And ApiMyPathItems uses @Pattern and other validation decorators
}

However, in the case where the items are bare strings, then it does not work:

type: array
items:
  type: string
  pattern: ^abc$

Will not validate anything.

public class InlineObject {
  public static final String SERIALIZED_NAME_ITEMS = "items";
  @SerializedName(SERIALIZED_NAME_ITEMS)
  private List<String> items = null;

  // cut for clarity
  @javax.annotation.Nullable
  @ApiModelProperty(value = "")
  // No validation!
  public List<String> getItems() {
    return items;
  }
}

I would expect it to do something, as the OpenAPI 3.0 spec seems to allow the use of pattern in an array's items (emphasis by me):

items - Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema. items MUST be present if the type is array.

openapi-generator version

Current release (4.2.3-SNAPSHOT)

OpenAPI declaration file content or url

In src/main/resource/api.yaml:

openapi: "3.0.0"
info:
  title: Test
  version: v1
servers:
  - url: https://test.com
paths:
  /api/my/path:
    post:
      operationId: myOperation
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                items:
                  type: array
                  items:
                    type: string
                    pattern: ^[a-z]{1}$
                    maxLength: 1
      responses:
        '200':
          description: success
          content:
            text/plain:
              schema:
                type: string

Command line used for generation

I'm using the following pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>nicolas.couvrat</groupId>
  <artifactId>test-openapi-codegen</artifactId>
  <version>1.0-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>4.2.3-SNAPSHOT</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
              <generatorName>java</generatorName>
              <configOptions>
                <sourceFolder>src/gen/java/main</sourceFolder>
                <useBeanValidation>true</useBeanValidation>
                <performBeanValidation>true</performBeanValidation>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <pluginRepositories>
    <pluginRepository>
      <id>sonatype-snapshots</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </pluginRepository>
  </pluginRepositories>
</project>
Steps to reproduce
mkdir -p src/main/resources
# put the spec in src/main/resources/api.yaml
# put the pom in pom.xml
mvn clean compile

^ The above will not compile but files will be generated, then checking the generated code shows the issue (in InlineObject.java).

Related issues/PRs

N/A

Suggest a fix

I'm all up to search for a way to solve this, but I would first like to know if this is intended or no. I have noticed that other tools, like Redoc, also do not seem to support this (the validation is not shown for an array of strings).

I first want to know if this is expected or no? Once again, as mentioned above, the open api spec seems to allow it.

Bug

Most helpful comment

All 2 comments

Just FYI we now have the data at the java layer to allow validation of schemas inside ArrayModel or array (model property, endpoint parameter, endpoint response). So if someone wants to add this feature they can.
This PR added the missing info in the Java layer: https://github.com/OpenAPITools/openapi-generator/pull/7621

Was this page helpful?
0 / 5 - 0 ratings