Skip to content

Commit 31f29be

Browse files
ePaulwing328
authored andcommitted
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.
1 parent b2aa877 commit 31f29be

File tree

14 files changed

+254
-180
lines changed

14 files changed

+254
-180
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/examples/ExampleGenerator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.HashSet;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.Random;
3536
import java.util.Set;
3637

3738
public class ExampleGenerator {
@@ -47,9 +48,12 @@ public class ExampleGenerator {
4748
private static final String NONE = "none";
4849

4950
protected Map<String, Model> examples;
51+
private Random random;
5052

5153
public ExampleGenerator(Map<String, Model> examples) {
5254
this.examples = examples;
55+
// use a fixed seed to make the "random" numbers reproducible.
56+
this.random = new Random("ExampleGenerator".hashCode());
5357
}
5458

5559
public List<Map<String, String>> generate(Map<String, Object> examples, List<String> mediaTypes, Property property) {
@@ -187,13 +191,13 @@ private Object resolvePropertyToExample(String mediaType, Property property, Set
187191
private double randomNumber(Double min, Double max) {
188192
if (min != null && max != null) {
189193
double range = max - min;
190-
return Math.random() * range + min;
194+
return random.nextDouble() * range + min;
191195
} else if (min != null) {
192-
return Math.random() + min;
196+
return random.nextDouble() + min;
193197
} else if (max != null) {
194-
return Math.random() * max;
198+
return random.nextDouble() * max;
195199
} else {
196-
return Math.random() * 10;
200+
return random.nextDouble() * 10;
197201
}
198202
}
199203

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md

samples/server/petstore/nodejs-google-cloud-functions/api/swagger.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ paths:
108108
type: "array"
109109
items:
110110
type: "string"
111-
default: "available"
112111
enum:
113112
- "available"
114113
- "pending"
115114
- "sold"
115+
default: "available"
116116
collectionFormat: "csv"
117117
responses:
118118
200:
@@ -385,7 +385,6 @@ paths:
385385
description: "ID of the order that needs to be deleted"
386386
required: true
387387
type: "string"
388-
minimum: 1
389388
responses:
390389
400:
391390
description: "Invalid ID supplied"

samples/server/petstore/nodejs-google-cloud-functions/controllers/Pet.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
var url = require('url');
44

5-
65
var Pet = require('./PetService');
76

8-
97
module.exports.addPet = function addPet (req, res, next) {
108
Pet.addPet(req.swagger.params, res, next);
119
};

samples/server/petstore/nodejs-google-cloud-functions/controllers/PetService.js

Lines changed: 78 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,153 +2,165 @@
22

33
exports.addPet = function(args, res, next) {
44
/**
5-
* parameters expected in the args:
6-
* body (Pet)
7-
**/
8-
// no response value expected for this operation
5+
* Add a new pet to the store
6+
*
7+
*
8+
* body Pet Pet object that needs to be added to the store
9+
* no response value expected for this operation
10+
**/
911
res.end();
1012
}
1113

1214
exports.deletePet = function(args, res, next) {
1315
/**
14-
* parameters expected in the args:
15-
* petId (Long)
16-
* api_key (String)
17-
**/
18-
// no response value expected for this operation
16+
* Deletes a pet
17+
*
18+
*
19+
* petId Long Pet id to delete
20+
* api_key String (optional)
21+
* no response value expected for this operation
22+
**/
1923
res.end();
2024
}
2125

2226
exports.findPetsByStatus = function(args, res, next) {
2327
/**
24-
* parameters expected in the args:
25-
* status (List)
26-
**/
27-
var examples = {};
28+
* Finds Pets by status
29+
* Multiple status values can be provided with comma separated strings
30+
*
31+
* status List Status values that need to be considered for filter
32+
* returns List
33+
**/
34+
var examples = {};
2835
examples['application/json'] = [ {
2936
"photoUrls" : [ "aeiou" ],
3037
"name" : "doggie",
31-
"id" : 123456789,
38+
"id" : 0,
3239
"category" : {
3340
"name" : "aeiou",
34-
"id" : 123456789
41+
"id" : 6
3542
},
3643
"tags" : [ {
3744
"name" : "aeiou",
38-
"id" : 123456789
45+
"id" : 1
3946
} ],
40-
"status" : "aeiou"
47+
"status" : "available"
4148
} ];
42-
if(Object.keys(examples).length > 0) {
49+
if (Object.keys(examples).length > 0) {
4350
res.setHeader('Content-Type', 'application/json');
4451
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
45-
}
46-
else {
52+
} else {
4753
res.end();
4854
}
49-
5055
}
5156

5257
exports.findPetsByTags = function(args, res, next) {
5358
/**
54-
* parameters expected in the args:
55-
* tags (List)
56-
**/
57-
var examples = {};
59+
* Finds Pets by tags
60+
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
61+
*
62+
* tags List Tags to filter by
63+
* returns List
64+
**/
65+
var examples = {};
5866
examples['application/json'] = [ {
5967
"photoUrls" : [ "aeiou" ],
6068
"name" : "doggie",
61-
"id" : 123456789,
69+
"id" : 0,
6270
"category" : {
6371
"name" : "aeiou",
64-
"id" : 123456789
72+
"id" : 6
6573
},
6674
"tags" : [ {
6775
"name" : "aeiou",
68-
"id" : 123456789
76+
"id" : 1
6977
} ],
70-
"status" : "aeiou"
78+
"status" : "available"
7179
} ];
72-
if(Object.keys(examples).length > 0) {
80+
if (Object.keys(examples).length > 0) {
7381
res.setHeader('Content-Type', 'application/json');
7482
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
75-
}
76-
else {
83+
} else {
7784
res.end();
7885
}
79-
8086
}
8187

8288
exports.getPetById = function(args, res, next) {
8389
/**
84-
* parameters expected in the args:
85-
* petId (Long)
86-
**/
87-
var examples = {};
90+
* Find pet by ID
91+
* Returns a single pet
92+
*
93+
* petId Long ID of pet to return
94+
* returns Pet
95+
**/
96+
var examples = {};
8897
examples['application/json'] = {
8998
"photoUrls" : [ "aeiou" ],
9099
"name" : "doggie",
91-
"id" : 123456789,
100+
"id" : 0,
92101
"category" : {
93102
"name" : "aeiou",
94-
"id" : 123456789
103+
"id" : 6
95104
},
96105
"tags" : [ {
97106
"name" : "aeiou",
98-
"id" : 123456789
107+
"id" : 1
99108
} ],
100-
"status" : "aeiou"
109+
"status" : "available"
101110
};
102-
if(Object.keys(examples).length > 0) {
111+
if (Object.keys(examples).length > 0) {
103112
res.setHeader('Content-Type', 'application/json');
104113
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
105-
}
106-
else {
114+
} else {
107115
res.end();
108116
}
109-
110117
}
111118

112119
exports.updatePet = function(args, res, next) {
113120
/**
114-
* parameters expected in the args:
115-
* body (Pet)
116-
**/
117-
// no response value expected for this operation
121+
* Update an existing pet
122+
*
123+
*
124+
* body Pet Pet object that needs to be added to the store
125+
* no response value expected for this operation
126+
**/
118127
res.end();
119128
}
120129

121130
exports.updatePetWithForm = function(args, res, next) {
122131
/**
123-
* parameters expected in the args:
124-
* petId (Long)
125-
* name (String)
126-
* status (String)
127-
**/
128-
// no response value expected for this operation
132+
* Updates a pet in the store with form data
133+
*
134+
*
135+
* petId Long ID of pet that needs to be updated
136+
* name String Updated name of the pet (optional)
137+
* status String Updated status of the pet (optional)
138+
* no response value expected for this operation
139+
**/
129140
res.end();
130141
}
131142

132143
exports.uploadFile = function(args, res, next) {
133144
/**
134-
* parameters expected in the args:
135-
* petId (Long)
136-
* additionalMetadata (String)
137-
* file (File)
138-
**/
139-
var examples = {};
145+
* uploads an image
146+
*
147+
*
148+
* petId Long ID of pet to update
149+
* additionalMetadata String Additional data to pass to server (optional)
150+
* file File file to upload (optional)
151+
* returns ApiResponse
152+
**/
153+
var examples = {};
140154
examples['application/json'] = {
141-
"code" : 123,
155+
"code" : 0,
142156
"type" : "aeiou",
143157
"message" : "aeiou"
144158
};
145-
if(Object.keys(examples).length > 0) {
159+
if (Object.keys(examples).length > 0) {
146160
res.setHeader('Content-Type', 'application/json');
147161
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
148-
}
149-
else {
162+
} else {
150163
res.end();
151164
}
152-
153165
}
154166

samples/server/petstore/nodejs-google-cloud-functions/controllers/Store.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
var url = require('url');
44

5-
65
var Store = require('./StoreService');
76

8-
97
module.exports.deleteOrder = function deleteOrder (req, res, next) {
108
Store.deleteOrder(req.swagger.params, res, next);
119
};

0 commit comments

Comments
 (0)