Comments (12)
this was recently clarified in the upcoming 3.0.4 patch release
https://github.com/handrews/OpenAPI-Specification/blob/4a46b20c451de02e5dee9974fbcbb9be24098d10/versions/3.0.4.md#options-for-mapping-values-to-schemas
related: OAI/OpenAPI-Specification#2520
from redocly-cli.
Okay, so to clarify, here's the three pieces I got from those links:
Implicit Mapping:
If a mapping
section is not provided, the value of each key will be used to map to a schema found under the Component section of the spec.
Explicit Mapping - Value:
If the mapping section is provided and the value DOES NOT being with ./
then it will be inferred as a name for a schema found under the Component section of the spec
Explicit Mapping - Reference:
If the mapping section is provided and the value DOES being with ./
then it will be inferred as a reference that needs to be resolved
So assuming the 3 statements above are correct, it sounds like we just need to prepend ./
to all of our file references?
from redocly-cli.
I just spoke to one of my teammates, and the spec that's live at the moment (which doesn't work) has the ./
prepended like
discriminator:
propertyName: type
mapping:
SCHEMA_A: ./schema_a.yaml
SCHEMA_B: ./schema_b.yaml
SCHEMA_C: ./schema_c.yaml
oneOf:
- $ref: ./schema_a.yaml
- $ref: ./schema_b.yaml
- $ref: ./schema_c.yaml
When we bundle using the Redocly CLI, the $refs are resolved in the oneOf
section, but the mapping section remains as is and the result is those are now no longer valid file paths.
from redocly-cli.
Ahh wait, I think I'm understanding now. The definition for this mapping section is for a "relative reference" not a JSON reference which is what the $ref
is.
Unless specified otherwise, all properties that are URLs MAY be relative references as defined by RFC3986. Relative references are resolved using the URLs defined in the Server Object as a Base URI.
Relative references used in $ref are processed as per JSON Reference, using the URL of the current document as the base URI. See also the Reference Object.
So it sounds like we can't actually use file references here since it's not using the "JSON Reference" and instead must be relative to the Server URI?
from redocly-cli.
from redocly-cli.
So if that understanding is correct, that essentially means we can't use discriminators with the redocly bundler if we have our schema split across multiple files? We tried the following cases and all of them were invalid. Note that SCHEMA_A, SCHEMA_B, and SCHEMA_C are all under the components schemas section.
Schema Name Approach:
Issue: Invalid $ref from Redocly bundler and linter
Redocly was reporting that SCHEM_A (and the others) was an invalid references
discriminator:
propertyName: type
mapping:
SCHEMA_A: SCHEMA_A
SCHEMA_B: SCHEMA_B
SCHEMA_C: SCHEMA_C
oneOf:
- $ref: ./schema_a.yaml
- $ref: ./schema_b.yaml
- $ref: ./schema_c.yaml
Relative Reference Approach:
Issue: Invalid $ref from Redocly bundler and linter
Redocly was reporting that SCHEM_A (and the others) was an invalid references. I'm guessing this approach doesn't work because we're in a separate file so the # isn't starting from the openapi.yaml? It may work in a single, large file?
discriminator:
propertyName: type
mapping:
SCHEMA_A: '#/components/schemas/SCHEMA_A'
SCHEMA_B: '#/components/schemas/SCHEMA_B'
SCHEMA_C: '#/components/schemas/SCHEMA_C'
oneOf:
- $ref: ./schema_a.yaml
- $ref: ./schema_b.yaml
- $ref: ./schema_c.yaml
Server Reference Approach:
Issue: Invalid $ref from Redocly bundler and linter
Redocly was reporting that SCHEM_A (and the others) was an invalid references. When I tried this, it was complaining that the file/directory doesn't exist
discriminator:
propertyName: type
mapping:
SCHEMA_A: './components/schemas/SCHEMA_A'
SCHEMA_B: './components/schemas/SCHEMA_B'
SCHEMA_C: './components/schemas/SCHEMA_C'
oneOf:
- $ref: ./schema_a.yaml
- $ref: ./schema_b.yaml
- $ref: ./schema_c.yaml
Server Reference V2 Approach:
Issue: Invalid $ref from Redocly bundler and linter
Redocly was reporting that SCHEM_A (and the others) was an invalid references. I was just guessing at this point
discriminator:
propertyName: type
mapping:
SCHEMA_A: './#components/schemas/SCHEMA_A'
SCHEMA_B: './#components/schemas/SCHEMA_B'
SCHEMA_C: './#components/schemas/SCHEMA_C'
oneOf:
- $ref: ./schema_a.yaml
- $ref: ./schema_b.yaml
- $ref: ./schema_c.yaml
Are you able to get a valid example working that I can use as a reference? I'm not sure how to get this working where my schemas are split across a number of local files and I'm using Redocly to bundle them into a single large file.
from redocly-cli.
You can create a dummy schema in components schemas with a ref to each of those mapping schemas and then use the plain string reference mapping value
...
discriminator:
mapping:
A: schema_a
...
components:
schemas:
schema_a:
$ref: './schema_a.yaml'
It should resolve this schema and the discriminator correctly.
I'm on mobile so can't test it, but it should work
from redocly-cli.
Right, but that's using a single OpenAPI file right? We have our spec split across multiple directories and bundle them into a single file in our docker image.
from redocly-cli.
You should be able to add them to the root file and they will be resolved and it should work when bundled
from redocly-cli.
Interesting. It still creates the components
section for a --dereferenced
bundle. So it should be possible to reference those schemas directly.
I slightly modified the example:
openapi.yaml:
openapi: 3.1.0
info: {}
paths:
/test:
get:
responses:
default:
content:
application/json:
schema:
$ref: example/my-example.yaml
example/my-example.yaml:
discriminator:
propertyName: type
mapping:
VAL_1: ../values/val1.yaml
VAL_2: ../values/val2.yaml
allOf:
- $ref: ../values/val1.yaml
- $ref: ../values/val2.yaml
This produces the following bundle (please notice the comments about replacement):
openapi: 3.1.0
info: {}
paths:
/test:
get:
responses:
default:
content:
application/json:
schema:
discriminator: &ref_2
propertyName: type
mapping:
VAL_1: ../values/val1.yaml # Replace this with '#/components/schemas/val1'!
VAL_2: ../values/val2.yaml # Replace this with '#/components/schemas/val2'!
allOf: &ref_3
- type: object
properties: &ref_0
type:
type: string
name:
type: string
- type: object
properties: &ref_1
type:
type: string
address:
type: string
components:
schemas:
val1:
type: object
properties: *ref_0
val2:
type: object
properties: *ref_1
my-example:
discriminator: *ref_2
allOf: *ref_3
@its-hammer-time is this how you think it should work?
from redocly-cli.
@its-hammer-time is this how you think it should work?
Yeah exactly! Either swap it for #/components/...
or resolve the references inline much like what happens under allOf. I'm not sure what would be more appropriate, but something needs to happen as the local file reference is no longer valid if the spec is bundled somewhere else.
from redocly-cli.
Hey team, any update on this?
from redocly-cli.
Related Issues (20)
- Example validation is not done against the schema HOT 7
- bundle command converts floating point numbers to integers HOT 1
- Self-referencing circular pointer HOT 2
- Allow specifying an overwrite conflict strategy for the `join` command HOT 1
- Add docs for Arazzo linting
- Linting does not catch default value types not conforming to specified field type HOT 2
- Translations and eject command wrapper
- Using curl instead of the cli interface HOT 2
- Add `boolean-schema-prefixes` (analogue to `boolean-parameter-prefixes`) rule to schema/model validation HOT 2
- root level paths `/` are not split correctly HOT 1
- Suppress update now logging HOT 1
- Pattern properties casing triggers error after upgrade, possible regression with 1.18.1 release HOT 1
- [OAS 3 spec] License object `identifier` and `url` keys are mutually-exclusive HOT 6
- Cannot find module './types/arazzo' HOT 6
- Remove all usages of component marked x-internal HOT 6
- Prerendering docs followed by build command build-docs HOT 4
- Rule no-invalid-media-type-examples check fails on example with combination of two anyOf objects HOT 5
- Remove `node-fetch` package and use Node.js 18 native`fetch` HOT 2
- Update per-API settings
- Question regarding skipping preprocessors and decorators during bundle operation HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redocly-cli.