Skip to content

Commit cc8f51d

Browse files
authored
Merge pull request #189 from amarzavery/enumStrType
Ensure discriminator property has the correct enum constant while resolving it.
2 parents 18461be + 3bdc7d5 commit cc8f51d

File tree

5 files changed

+75
-44
lines changed

5 files changed

+75
-44
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 12/4/2017 0.4.21
2+
- Remove the enum constraint or reference to an enum on the discriminator property if previously present before making it a constant.
3+
14
### 11/20/2017 0.4.20
25
- Added support for processing [`"x-ms-parameterized-host": {}`](https://github.com/Azure/autorest/tree/master/docs/extensions#x-ms-parameterized-host) extension if present in the 2.0 swagger spec.
36

lib/validators/specResolver.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,18 @@ class SpecResolver {
660660
if (definitions[name]['x-ms-discriminator-value']) {
661661
val = definitions[name]['x-ms-discriminator-value'];
662662
}
663+
// Ensure that the property marked as a discriminator has only one value in the enum constraint for that model and it
664+
// should be the one that is the model name or the value indicated by x-ms-discriminator-value. This will make the discriminator
665+
// property a constant (in json schema terms).
666+
if (definitions[name].properties[discriminator]['$ref']) {
667+
delete definitions[name].properties[discriminator]['$ref'];
668+
}
669+
// We will set "type" to "string". It is safe to assume that properties marked as "discriminator" will be of type "string"
670+
// as it needs to refer to a model definition name. Model name would be a key in the definitions object/dictionary in the
671+
// swagger spec. keys would always be a string in a JSON object/dictionary.
672+
if (!definitions[name].properties[discriminator].type) {
673+
definitions[name].properties[discriminator].type = 'string';
674+
}
663675
definitions[name].properties[discriminator].enum = [`${val}`];
664676
}
665677

package-lock.json

Lines changed: 43 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oav",
3-
"version": "0.4.20",
3+
"version": "0.4.21",
44
"author": {
55
"name": "Microsoft Corporation",
66
"email": "[email protected]",

test/modelValidation/swaggers/specification/polymorphic/polymorphicSwagger.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
"type": "string"
233233
},
234234
"animalType": {
235-
"type": "string",
235+
"$ref": "#/definitions/AnimalType",
236236
"description": "Type of activity."
237237
},
238238
"siblings": {
@@ -292,6 +292,16 @@
292292
}
293293
}
294294
},
295+
"AnimalType": {
296+
"description": "Animal type.",
297+
"type": "string",
298+
"enum": [
299+
"Tiger",
300+
"Cat",
301+
"Mouse",
302+
"Mammal"
303+
]
304+
},
295305
"Tiger": {
296306
"description": "class for Tiger.",
297307
"allOf": [
@@ -315,7 +325,11 @@
315325
},
316326
"foodType": {
317327
"type": "string",
318-
"description": "Type of food."
328+
"description": "Type of food.",
329+
"enum": [
330+
"Meat",
331+
"Jain"
332+
]
319333
}
320334
},
321335
"required": [

0 commit comments

Comments
 (0)