Skip to content

Commit

Permalink
Fix issue parsing polymorphic schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Apr 24, 2024
1 parent a5f0805 commit de67233
Show file tree
Hide file tree
Showing 9 changed files with 29,207 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/autorest.bicep/src/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,32 @@ 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)) {
const childSchema = parseObjectType(putSubType, ancestorsToExclude);

if (!putSubType.discriminatorValue) {
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
Loading

0 comments on commit de67233

Please sign in to comment.