OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java] Add support for validation of strings in arrays
Nobody has claimed this yet.
- 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?
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reported mvn clean compile setup with src/main/resources/api.yaml and pom.xml, then inspect the generated InlineObject.java. Trace how the Java generator emits validation annotations for array items and compare string-item output with nested-object output. Done means generated lists of strings carry the declared pattern and length validation and the relevant generator tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100