Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/seven-emus-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Fixed an issue where discriminator `defaultMapping` was not resolved when bundling.
37 changes: 25 additions & 12 deletions packages/core/src/bundle/bundle-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../ref-utils.js';
import { type ResolvedRefMap, type Document } from '../resolve.js';
import { reportUnresolvedRef } from '../rules/common/no-unresolved-refs.js';
import { type OasRef } from '../typings/openapi.js';
import { type OasRef, type Oas3Discriminator } from '../typings/openapi.js';
import { dequal } from '../utils/dequal.js';
import { isTruthy } from '../utils/is-truthy.js';
import { makeRefId } from '../utils/make-ref-id.js';
Expand Down Expand Up @@ -205,19 +205,32 @@ export function makeBundleVisitor({
};

if (version === 'oas3') {
visitor.DiscriminatorMapping = {
leave(mapping: Record<string, string>, ctx: UserContext) {
for (const name of Object.keys(mapping)) {
const $ref = mapping[name];
const resolved = ctx.resolve({ $ref });
if (!resolved.location || resolved.node === undefined) {
reportUnresolvedRef(resolved, ctx.report, ctx.location.child(name));
return;
}
const componentType = mapTypeToComponent('Schema', version)!;
visitor.Discriminator = {
leave(discriminator: Oas3Discriminator, ctx: UserContext) {
if (typeof discriminator.defaultMapping !== 'string') return;

const componentType = mapTypeToComponent('Schema', version)!;
mapping[name] = saveComponent(componentType, resolved, ctx);
const resolved = ctx.resolve({ $ref: discriminator.defaultMapping });
if (!resolved.location || resolved.node === undefined) {
reportUnresolvedRef(resolved, ctx.report, ctx.location.child('defaultMapping'));
return;
}

discriminator.defaultMapping = saveComponent(componentType, resolved, ctx);
},
DiscriminatorMapping: {
leave(mapping, ctx) {
for (const name of Object.keys(mapping)) {
const $ref = mapping[name];
const resolved = ctx.resolve({ $ref });
if (!resolved.location || resolved.node === undefined) {
reportUnresolvedRef(resolved, ctx.report, ctx.location.child(name));
return;
}

mapping[name] = saveComponent(componentType, resolved, ctx);
}
},
},
};
}
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/bundle/bundle-oas3_2/components/schemas/Foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
allOf:
- $ref: ./Used.yaml
- type: object
properties:
test:
type: string
const: foo
required:
- test
7 changes: 7 additions & 0 deletions tests/e2e/bundle/bundle-oas3_2/components/schemas/Used.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: object
properties:
base:
type: string
discriminator:
propertyName: test
defaultMapping: ./Foo.yaml
6 changes: 6 additions & 0 deletions tests/e2e/bundle/bundle-oas3_2/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
openapi: 3.2.0
info: {}
servers: []
paths:
/:
$ref: paths/_.yaml
7 changes: 7 additions & 0 deletions tests/e2e/bundle/bundle-oas3_2/paths/_.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
get:
responses:
'200':
content:
application/json:
schema:
$ref: ../components/schemas/Used.yaml
3 changes: 3 additions & 0 deletions tests/e2e/bundle/bundle-oas3_2/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apis:
main:
root: ./openapi.yaml
35 changes: 35 additions & 0 deletions tests/e2e/bundle/bundle-oas3_2/snapshot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.2.0
info: {}
servers: []
paths:
/:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Used'
components:
schemas:
Used:
type: object
properties:
base:
type: string
discriminator:
propertyName: test
defaultMapping: '#/components/schemas/Foo'
Foo:
allOf:
- $ref: '#/components/schemas/Used'
- type: object
properties:
test:
type: string
const: foo
required:
- test

bundling openapi.yaml using configuration for api 'main'...
📦 Created a bundle for openapi.yaml at stdout <test>ms.
Loading