Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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]+$