diff --git a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java index 8f51864e95..175aa55946 100644 --- a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java +++ b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java @@ -27,6 +27,7 @@ import io.swagger.models.properties.ObjectProperty; import io.swagger.models.properties.Property; import io.swagger.models.properties.RefProperty; +import io.swagger.models.properties.StringProperty; import io.swagger.parser.SwaggerParser; import io.swagger.parser.SwaggerResolver; import io.swagger.parser.util.SwaggerDeserializationResult; @@ -873,6 +874,14 @@ private Schema convert(Property schema) { result.setExclusiveMaximum(np.getExclusiveMaximum()); result.setExclusiveMinimum(np.getExclusiveMinimum()); } + + if (schema instanceof StringProperty) { + StringProperty sp = (StringProperty) schema; + + result.setMinLength(sp.getMinLength()); + result.setMaxLength(sp.getMaxLength()); + result.setPattern(sp.getPattern()); + } } if (schema.getVendorExtensions() != null) { diff --git a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java index b273e6931d..26275e9e77 100644 --- a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java +++ b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java @@ -74,6 +74,7 @@ public class V2ConverterTest { private static final String ISSUE_672_JSON = "issue-672.json"; private static final String ISSUE_673_YAML = "issue-673.yaml"; private static final String ISSUE_676_JSON = "issue-676.json"; + private static final String ISSUE_708_YAML = "issue-708.yaml"; private static final String API_BATCH_PATH = "/api/batch/"; private static final String PETS_PATH = "/pets"; @@ -600,7 +601,17 @@ public void testIssue673() throws Exception { assertEquals(2, schema.getMinLength().intValue()); assertEquals(7, schema.getMaxLength().intValue()); assertEquals("aaa", schema.getPattern()); + } + @Test(description = "OpenAPI v2 converter - Migrate minLength, maxLength and pattern of String property") + public void testIssue708() throws Exception { + final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_708_YAML); + assertNotNull(oas); + Schema schema = oas.getComponents().getSchemas().get("SomeObj"); + assertNotNull(schema); + assertEquals(schema.getMinLength(), Integer.valueOf(1)); + assertEquals(schema.getMaxLength(), Integer.valueOf(3)); + assertEquals(schema.getPattern(), "^[0-9]+$"); } private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException { diff --git a/modules/swagger-parser-v2-converter/src/test/resources/issue-708.yaml b/modules/swagger-parser-v2-converter/src/test/resources/issue-708.yaml new file mode 100644 index 0000000000..b22ec32536 --- /dev/null +++ b/modules/swagger-parser-v2-converter/src/test/resources/issue-708.yaml @@ -0,0 +1,37 @@ +swagger: '2.0' +info: + description: 'Test' + version: 1.0.0 + title: OpenAPI Test + license: + name: Apache-2.0 + url: 'http://www.apache.org/licenses/LICENSE-2.0.html' +host: petstore.swagger.io +basePath: /v2 +schemes: + - http +paths: + /ping: + post: + summary: test + description: 'test it' + operationId: pingOp + consumes: + - application/json + produces: + - application/json + parameters: + - in: body + name: body + required: true + schema: + $ref: '#/definitions/SomeObj' + responses: + '200': + description: OK +definitions: + SomeObj: + type: string + minLength: 1 + maxLength: 3 + pattern: ^[0-9]+$ \ No newline at end of file