luckslovez
luckslovez

Reputation: 31

Swagger Schema - Pattern not applied to string array

So I have a swagger schema described as:

MyStringArrayObject:
      properties:
        myStringArray:
          type: array
          minItems: 1
          items:
            type: string
            pattern: ^\d+-.*-.+\d+$
            maxLength: 100
      required:
        - myStringArray

My generated class looks as follows (I ommited constructor, hashCode, equals, etc):

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen")
public class MyStringArrayObject implements Serializable{
  @SerializedName("myStringArray")
  private List<String> myStringArray = new ArrayList<>();
  @NotNull
 @Size(min=1)  @Schema(required = true)
  public List<String> getMyStringArray() {
    return myStringArray;
  }
  public void setMyStringArray(List<String> myStringArray) {
    this.myStringArray = myStringArray;
  }
}

Why aren't the array (list) items validated with the pattern and maxLength defined? Is the plugin not capable of this? What am I missing?

In case it helps, here is my plugin configuration:

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.18</version>
    <executions>
        <execution>
            <id>execution-swagger-models</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>

                <language>java</language>
                <library>okhttp-gson</library>

                <output>${project.build.directory}/generated-sources/swagger</output>
                <configOptions>
                    <serializableModel>true</serializableModel>
                    <dateLibrary>java8-localdatetime</dateLibrary>
                    <useBeanValidation>true</useBeanValidation>
                    <sourceFolder>.</sourceFolder>
                </configOptions>
                <generateApis>false</generateApis>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateModelTests>false</generateModelTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <environmentVariables>
                    <!-- generate all models -->
                </environmentVariables>
                <inputSpec>${project.basedir}/src/main/resources/myProject.yml</inputSpec>
                <modelPackage>my.model</modelPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

Thanks in advance!

Upvotes: 2

Views: 3439

Answers (1)

Maniganda Prakash
Maniganda Prakash

Reputation: 4732

I did face that problem, hopefully this gets fixed in the future release of Swagger and OpenApi generators.

As a workaround wrap it inside another object, languages is the one am referring to.

Channel:
  type: object
  required:
    - channelName
  properties:
    channelAccountId:
      type: string
      format: UUID
      readOnly: true
    languages:
      type: array
      items:
        $ref: '#/components/schemas/Languages'


Languages:
  properties:
    language:
      type: string
      pattern: '[a-zA-Z]{2,3}([-\/][a-zA-Z]{2,3})?'

Classes are generated as follows.

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2021-06-29T16:57:18.931915-07:00[America/Phoenix]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "channelName", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = EMAIL.class, name = "email"),
  @JsonSubTypes.Type(value = RCS.class, name = "rcs"),
})

public class Channel   {
  @JsonProperty("channelAccountId")
  private UUID channelAccountId;

  @JsonProperty("languages")
  @Valid
  private List<Languages> languages = null;

  ....
}

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2021-06-29T16:57:18.931915-07:00[America/Phoenix]")
public class Languages   {
  @JsonProperty("language")
  private String language;

  public Languages language(String language) {
    this.language = language;
    return this;
  }
 
  @ApiModelProperty(value = "")
  @Pattern(regexp="[a-zA-Z]{2,3}([-/][a-zA-Z]{2,3})?") 
  public String getLanguage() {
    return language;
  }

  public void setLanguage(String language) {
    this.language = language;
  }
}

Upvotes: 1

Related Questions