joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

How to use RegEx pattern in schema while generation of POJO #1489

Closed ravibarkhani closed 1 year ago

ravibarkhani commented 1 year ago

Dear team, I am experiencing issue while generating RegEx pattern from maven plugin, Can you please suggest solution.

I would like to add RegEx pattern on String property but it is not generating required pattern using maven plugin. Same If I generate from website "https://www.jsonschema2pojo.org/" with "type": "string", "pattern" : "^[A-Z]{3}$" and select Validation annotations: javax.validation It generates required RegEx on field.

With below mentioned schema example code
{ "type": "object", "properties": { "producer": { "type": "string", "pattern" : "^[A-Z]{3}$" } } } It generates Java class with plain String () @JsonProperty("producer") private String producer; @JsonIgnore

{ "type": "object", "properties": { "producer": { "type": "string", "format" : "regex", "pattern" : "^[A-Z]{3}$" } } } It generates Java class with plain String () @JsonProperty("producer") private Pattern producer;

Maven dependencies : `

com.fasterxml.jackson.core
            <artifactId>jackson-databind</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>

`

joelittlejohn commented 1 year ago

Can you please edit your comment to fix the formatting? I can't really tell what you're asking here I'm afraid.

unkish commented 1 year ago

Hi

I suppose that <includeJsr303Annotations>true</includeJsr303Annotations> might be missing in your maven configuration.

Given

            <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>jsonschema2pojo</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includeJsr303Annotations>true</includeJsr303Annotations>
                    <!-- Mandatory parameter -->
                    <sourceDirectory>${basedir}/src/main/resources</sourceDirectory>
                    <!-- Optional configuration to reduce amount of generated code -->
                    <includeHashcodeAndEquals>false</includeHashcodeAndEquals>
                    <includeToString>false</includeToString>
                    <includeAdditionalProperties>false</includeAdditionalProperties>
                </configuration>
            </plugin>

And test.json:

{
  "type": "object",
  "properties": {
    "producer": {
      "type": "string",
      "pattern": "^[A-Z]{3}$"
    }
  }
}

Generates:


package json;

import javax.annotation.processing.Generated;
import javax.validation.constraints.Pattern;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "producer"
})
@Generated("jsonschema2pojo")
public class Test {

    @JsonProperty("producer")
    @Pattern(regexp = "^[A-Z]{3}$")
    private String producer;

    @JsonProperty("producer")
    public String getProducer() {
        return producer;
    }

    @JsonProperty("producer")
    public void setProducer(String producer) {
        this.producer = producer;
    }

}

To get similar result on https://www.jsonschema2pojo.org you'd have to select either javax.validation or jakarta.validation in Validation annotations: section

ravibarkhani commented 1 year ago

Have verified this works