Fix an issue with example generator when array is too large (#46)

* fix issue with example generator when array is too large

* reformat code
This commit is contained in:
William Cheng 2018-05-15 23:08:37 +08:00 committed by GitHub
parent 70e9e2fb7e
commit 803821e210
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,10 +17,10 @@
package org.openapitools.codegen.examples; package org.openapitools.codegen.examples;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.core.util.Json;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -191,6 +191,10 @@ public class ExampleGenerator {
Schema innerType = ((ArraySchema) property).getItems(); Schema innerType = ((ArraySchema) property).getItems();
if (innerType != null) { if (innerType != null) {
int arrayLength = null == ((ArraySchema) property).getMaxItems() ? 2 : ((ArraySchema) property).getMaxItems(); int arrayLength = null == ((ArraySchema) property).getMaxItems() ? 2 : ((ArraySchema) property).getMaxItems();
if (arrayLength > 1024) {
LOGGER.warn("The max items allowed in property {} is too large ({} items), restricting it to 1024 items", property, arrayLength);
arrayLength = 1024;
}
Object[] objectProperties = new Object[arrayLength]; Object[] objectProperties = new Object[arrayLength];
Object objProperty = resolvePropertyToExample(propertyName, mediaType, innerType, processedModels); Object objProperty = resolvePropertyToExample(propertyName, mediaType, innerType, processedModels);
for (int i = 0; i < arrayLength; i++) { for (int i = 0; i < arrayLength; i++) {