-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OAS 3.1 for JSON Schemas #530
Conversation
There's some really ugly typing going on in here, but let's get the typescript conversion over before we do code cleanup. I gave code cleanup a shot and it was really problematic. Thanks for the help at the end there @erunion |
JSONSchema7, | ||
JSONSchema7Definition, | ||
JSONSchema7TypeName, | ||
} from '@typescript-eslint/experimental-utils/dist/json-schema'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be @types/json-schema
? If not, can that dependency get pulled from package.json
?
__tests__/operation.test.ts
Outdated
expect(operation.api).toStrictEqual(petstore); | ||
expect(operation['api']).toStrictEqual(petstore); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the change to not use dot notation here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At one point api
was private, and this bypasses typescript's public/private check. It looks like it's no longer private though so it works with dot notation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's also a getApi()
accessor on Operation
if you want to use that instead.
src/lib/openapi-to-json-schema.ts
Outdated
@@ -147,7 +164,9 @@ function searchForExampleByPointer(pointer, examples = []) { | |||
} | |||
|
|||
// Prevent us from crashing if `examples` is a completely empty object. | |||
const ex = schema.examples[keys.shift()]; | |||
const ex = schema.examples[keys.shift() as unknown as number]; | |||
// const ex = schema.examples[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// const ex = schema.examples[0]; |
src/lib/openapi-to-json-schema.ts
Outdated
schema.enum = [...new Set(schema.enum)]; | ||
if (RMOAS.isSchema(schema, isPolymorphicAllOfChild) && 'enum' in schema && Array.isArray(schema.enum)) { | ||
// If we ever target ES6 for typescript we can drop this array.from. https://stackoverflow.com/questions/33464504/using-spread-syntax-and-new-set-with-typescript/56870548 | ||
schema.enum = [...Array.from(new Set(schema.enum))]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need to do the spread operator here.
schema.enum = [...Array.from(new Set(schema.enum))]; | |
schema.enum = Array.from(new Set(schema.enum)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird, that all came from fixing some typing issues, looks to still be fine typewise without it.
src/operation.ts
Outdated
identifier: string; | ||
|
||
constructor( | ||
api: RMOAS.OASDocument, | ||
oas: Oas | RMOAS.OASDocument, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems off because the OAS constructor can't accept a copy of itself as its API definition, and we only ever pass this.api
when we create a new instance of this class.
}); | ||
} | ||
|
||
const oasTags = Object.fromEntries(oasTagMap); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the change to use Object.fromEntries
here instead of checking .has()
on the map?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember specifically, but I'm sure it's typing related. Happy to mess around with restoring the old version tomorrow morning if you'd like.
if (typeof components[componentType] === 'undefined') { | ||
components[componentType] = {}; | ||
} | ||
// Typescript is INCREDIBLY SLOW parsing this one line. I think it's because of the large variety of types that that object could represent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Typescript is INCREDIBLY SLOW parsing this one line. I think it's because of the large variety of types that that object could represent | |
// @fixme Typescript is INCREDIBLY SLOW parsing this one line. I think it's because of the large variety of types that that object could represent |
if (param.name && param2.name) { | ||
return param.name === param2.name && param.in === param2.in; | ||
} else if (param.$ref && param2.$ref) { | ||
if ((param as RMOAS.ParameterObject).name && (param2 as RMOAS.ParameterObject).name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type: 'object', | ||
properties: {}, | ||
}; | ||
|
||
Object.keys(headers).forEach(key => { | ||
if (headers[key] && headers[key].schema) { | ||
if (headers[key] && (headers[key] as OpenAPIV3.HeaderObject | OpenAPIV3_1.HeaderObject).schema) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to make a HeaderObject
in rmoas.types
that shortcuts having to repeat OpenAPIV3.HeaderObject | OpenAPIV3_1.HeaderObject
.
I also made a couple changes for minor things I found, you can see all my changes here: https://github.com/readmeio/oas/compare//58d150e746f52e9a25fdfa4d27424ebce94b412d...1841ce8?expand=1
|
Just pushed up fixes for everything. LMK what you think. |
🧰 Changes
This is all the work necessary to support OAS 3.1 for JSON schemas.
pre OAS 3.1 your JSON schema could only be draft
04
. 3.1 your JSON schema can be anything, as determined by the$schema
value in your schema, the value ofjsonSchemaDialect
or the default, draft2020-12
.These updates to the OAS library now ensure that the schemas you are working with always have the accurate
$schema
value, following the previously mentioned chain of priorities.Along the way I converted the following to typescript
🧬 QA & Testing