forked from loafle/openapi-generator-original
Use deterministic randomness in ExampleGenerator. (#5068)
* Update samples for nodejs + nodejs-google-cloud-functions. * Fix example generator to use deterministic randomness. This avoids changing results after each generation, and makes diff reviews easier. * Update NodeJS samples. This is the last "randomness" update. From now on the samples should only change if either the generator, the input or parameters change.
This commit is contained in:
@@ -32,6 +32,7 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
public class ExampleGenerator {
|
||||
@@ -47,9 +48,12 @@ public class ExampleGenerator {
|
||||
private static final String NONE = "none";
|
||||
|
||||
protected Map<String, Model> examples;
|
||||
private Random random;
|
||||
|
||||
public ExampleGenerator(Map<String, Model> examples) {
|
||||
this.examples = examples;
|
||||
// use a fixed seed to make the "random" numbers reproducible.
|
||||
this.random = new Random("ExampleGenerator".hashCode());
|
||||
}
|
||||
|
||||
public List<Map<String, String>> generate(Map<String, Object> examples, List<String> mediaTypes, Property property) {
|
||||
@@ -187,13 +191,13 @@ public class ExampleGenerator {
|
||||
private double randomNumber(Double min, Double max) {
|
||||
if (min != null && max != null) {
|
||||
double range = max - min;
|
||||
return Math.random() * range + min;
|
||||
return random.nextDouble() * range + min;
|
||||
} else if (min != null) {
|
||||
return Math.random() + min;
|
||||
return random.nextDouble() + min;
|
||||
} else if (max != null) {
|
||||
return Math.random() * max;
|
||||
return random.nextDouble() * max;
|
||||
} else {
|
||||
return Math.random() * 10;
|
||||
return random.nextDouble() * 10;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user