Skip to content

Commit

Permalink
Fix issue parsing polymorphic schemas (#2131)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin authored Apr 24, 2024
1 parent a5f0805 commit e8c2e62
Show file tree
Hide file tree
Showing 13 changed files with 7,866 additions and 67 deletions.
21 changes: 20 additions & 1 deletion src/autorest.bicep/src/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,33 @@ export function generateSchema(host: AutorestExtensionHost, definition: Provider
ancestorsToExclude.add(putSchema);
}

const discriminatorName = putSchema?.discriminator?.property.serializedName;

const oneOf: JSONSchema4[] = [];
for (const { putSubType } of getDiscriminatedSubTypes(putSchema)) {
for (const { subTypeName, putSubType } of getDiscriminatedSubTypes(putSchema)) {
const childSchema = parseObjectType(putSubType, ancestorsToExclude);

if (!putSubType.discriminatorValue) {
logWarning(`Found discriminated sub-type '${subTypeName}' without discriminator value`);
continue;
}

if (childSchema.type !== 'object') {
logWarning(`Found unexpected element of discriminated type '${childSchema.type}'`);
continue;
}

if (discriminatorName) {
childSchema.properties ??= {};
childSchema.properties[discriminatorName] = {
type: 'string',
enum: [putSubType.discriminatorValue],
};

childSchema.required = Array.isArray(childSchema.required) ? childSchema.required : [];
childSchema.required.push(discriminatorName);
}

oneOf.push(childSchema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
],
"type": "string"
},
"bar": {
"description": "The bar property",
"type": "string"
},
"foo": {
"description": "The foo property",
"type": "string"
},
"name": {
"description": "The discriminatedUnionTestType resource name.",
"type": "string"
},
"properties": {
"oneOf": [
{
"$ref": "#/definitions/DiscriminatedUnionTestTypeProperties"
},
{
"$ref": "https://schema.management.azure.com/schemas/common/definitions.json#/definitions/expression"
}
]
},
"type": {
"enum": [
"Test.Rp1/discriminatedUnionTestType"
Expand All @@ -34,6 +36,7 @@
},
"required": [
"name",
"properties",
"apiVersion",
"type"
],
Expand Down Expand Up @@ -294,6 +297,94 @@
}
},
"definitions": {
"DiscriminatedUnionTestTypeProperties": {
"oneOf": [
{
"properties": {
"baz": {
"description": "The baz property",
"type": "string"
},
"quux": {
"description": "A property defined inline",
"type": "string"
},
"type": {
"enum": [
"inherited"
],
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
},
{
"properties": {
"buzz": {
"description": "The buzz property",
"type": "string"
},
"fizz": {
"description": "The fizz property",
"type": "string"
},
"pop": {
"description": "The pop property",
"type": "string"
},
"type": {
"enum": [
"inline"
],
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
},
{
"properties": {
"foo": {
"description": "The overridden foo property",
"oneOf": [
{
"type": "integer"
},
{
"$ref": "https://schema.management.azure.com/schemas/common/definitions.json#/definitions/expression"
}
]
},
"type": {
"enum": [
"override"
],
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
}
],
"properties": {
"bar": {
"description": "The bar property",
"type": "string"
},
"foo": {
"description": "The foo property",
"type": "string"
}
},
"type": "object"
},
"EncryptionProperties": {
"description": "Configuration of key for data encryption",
"properties": {
Expand Down
Loading

0 comments on commit e8c2e62

Please sign in to comment.