Skip to content
Merged
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 the discriminator's `defaultMapping` property was not resolved when bundling.
32 changes: 32 additions & 0 deletions packages/core/src/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,38 @@ describe('bundle', () => {
expect(problems).toHaveLength(0);
expect(res.parsed).toMatchSnapshot();
});

it('should bundle discriminator with defaultMapping and mapping as component names to the same document', async () => {
const document = outdent`
openapi: 3.2.0
components:
schemas:
Pet:
type: object
discriminator:
propertyName: kind
defaultMapping: Cat
mapping:
cat: Cat
Cat:
type: object
properties:
kind:
type: string

`;

const {
bundle: { parsed },
problems,
} = await bundleFromString({
source: document,
config: await createConfig({}),
});

expect(problems).toMatchInlineSnapshot(`[]`);
expect(parsed).toMatchInlineSnapshot(document);
});
});

describe('bundleFromString', () => {
Expand Down
46 changes: 34 additions & 12 deletions packages/core/src/bundle/bundle-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
pointerBaseName,
refBaseName,
type Location,
isMappingRef,
} 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 { makeRefId } from '../utils/make-ref-id.js';
import { type Oas3Visitor, type Oas2Visitor } from '../visitors.js';
Expand Down Expand Up @@ -205,19 +206,40 @@ 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' ||
!isMappingRef(discriminator.defaultMapping)
) {
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];
if (!isMappingRef($ref)) {
continue;
}
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