Skip to content

Commit

Permalink
remove random console.log statements
Browse files Browse the repository at this point in the history
format fix-request-media-type-object-missing-schema

fix bug in fix-passthrough-refs

fix incorrect ref

publish getresponse
  • Loading branch information
dphuang2 committed Mar 28, 2024
1 parent b93c922 commit 9f96eea
Show file tree
Hide file tree
Showing 34 changed files with 159,247 additions and 1,369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export async function fixPassthroughRefs({
}

let passthroughRefs = collectPassthroughRefs()
console.log(passthroughRefs)
// iterate through all passthrough refs and replace them with the reffed schema
for (const [source, destination] of passthroughRefs) {
recurseObject(spec.spec, ({ value: schema }) => {
Expand All @@ -74,6 +73,10 @@ export async function fixPassthroughRefs({

// get name of schema from ref
const ref = schema['$ref']

// check that ref is referring to something with "components/schemas"
if (!ref.startsWith('#/components/schemas/')) return

const schemaName = ref.split('/').pop()
if (schemaName === undefined) return
// check if schemaName matches name of passthrough ref (e.g. "source")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { Spec, getOperations, resolveRef } from 'konfig-lib'

export async function fixRequestMediaTypeObjectMissingSchema({
spec,
}: {
spec: Spec
}): Promise<number> {
let numberOfMissingRequestSchemasInMediaTypeObjects = 0
getOperations({ spec: spec.spec }).forEach(({ operation }) => {
if (operation.requestBody) {
const requestBody = resolveRef({refOrObject: operation.requestBody, $ref: spec.$ref})
Object.values(requestBody.content).forEach((mediaTypeObject) => {
if (!mediaTypeObject.schema) {
mediaTypeObject.schema = { description: "WARNING: Missing schema in media type object. Missing schema has been filled with this AnyType schema." }
numberOfMissingRequestSchemasInMediaTypeObjects++
}
})
spec,
}: {
spec: Spec
}): Promise<number> {
let numberOfMissingRequestSchemasInMediaTypeObjects = 0
getOperations({ spec: spec.spec }).forEach(({ operation }) => {
if (operation.requestBody) {
const requestBody = resolveRef({
refOrObject: operation.requestBody,
$ref: spec.$ref,
})
if (requestBody.content === undefined || requestBody.content === null) {
throw Error(
`Missing content in request body for ${operation.operationId}`
)
}
})
return numberOfMissingRequestSchemasInMediaTypeObjects;
}
Object.values(requestBody.content).forEach((mediaTypeObject) => {
if (!mediaTypeObject.schema) {
mediaTypeObject.schema = {
description:
'WARNING: Missing schema in media type object. Missing schema has been filled with this AnyType schema.',
}
numberOfMissingRequestSchemasInMediaTypeObjects++
}
})
}
})
return numberOfMissingRequestSchemasInMediaTypeObjects
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function validateSchemaName({
const nameWithoutX = name.replace(/X/g, '')
const isCamelcase =
camelcase(nameWithoutX, { pascalCase: true }) === nameWithoutX
console.log(name, isCamelcase)
const isNonEmpty = nameSchema.safeParse(name).success
const isUnique =
spec.components === undefined ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fixPassthroughRefs request body shouldn't be shortened 1`] = `
{
"components": {
"requestBodies": {
"TestRequestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PassthroughSchema",
},
},
},
},
},
"schemas": {
"PassthroughSchema": {
"properties": {
"test": {
"type": "string",
},
},
"type": "object",
},
},
},
"info": {
"title": "Test API",
"version": "1.0.0",
},
"openapi": "3.0.0",
"paths": {
"/test": {
"post": {
"requestBody": {
"$ref": "#/components/requestBodies/RequestBody",
},
"responses": {
"200": {
"description": "Successful response",
},
},
},
},
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Spec, parseSpec, recurseObject } from 'konfig-lib'
import { fixPassthroughRefs } from '../../src/util/fix-passthrough-refs'

describe('fixPassthroughRefs', () => {
it(`request body shouldn't be shortened`, async () => {
const openapi: Spec['spec'] = {
openapi: '3.0.0',
info: {
title: 'Test API',
version: '1.0.0',
},
paths: {
'/test': {
post: {
requestBody: {
$ref: '#/components/requestBodies/RequestBody',
},
responses: {
'200': {
description: 'Successful response',
},
},
},
},
},
components: {
requestBodies: {
TestRequestBody: {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/RequestBody',
},
},
},
},
},
schemas: {
RequestBody: {
$ref: '#/components/schemas/PassthroughSchema',
},
PassthroughSchema: {
type: 'object',
properties: {
test: {
type: 'string',
},
},
},
},
},
}

const spec = await parseSpec(JSON.stringify(openapi))

const fixedSpec = await fixPassthroughRefs({ spec })

// snapshot expect
expect(spec.spec).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hash: 5bc3c66e5a79f8838fc3d59af06be08cb3bb71cb8b809eb0a652be12a58d7805
hash: 22ce7349507d865bb698fdc82fe8215978a81a02a5b2e9b9a2d57c2427a13f64
methodObjects:
- url: /authorize
method: authorize
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hash: 34dbab87b60c92915d6c3607dcc8bf6809b49decfea3b148ee3ad76080242646
hash: 26d7310c960ca6e2183ea9a808b5fe3e090effffc55221e04ca36c6005ab2cb5
methodObjects:
- url: /v2/1-clicks
method: list
Expand Down Expand Up @@ -9634,7 +9634,7 @@ apiDescription: >
how to modify a resource updating only specific attributes.|
|POST|To create a new object, your request should specify the POST method. The
POST request includes all of the attributes necessary to create a new object.
POST request includes all of the attributes necessary to create a new object.
When you wish to create a new object, send a POST request to the target
endpoint.|
Expand Down Expand Up @@ -9971,22 +9971,22 @@ apiDescription: >
If the `ratelimit-remaining` reaches zero, subsequent requests will receive
a 429 error code until the request reset has been reached.
a 429 error code until the request reset has been reached.
`ratelimit-remaining` reaching zero can also indicate that the "burst limit"
of 250
of 250
requests per minute limit was met, even if the 5,000 requests per hour limit
was not.
was not.
In this case, the 429 error response will include a retry-after header to
indicate how
indicate how
long to wait (in seconds) until the request may be retried.
You can see the format of the response in the examples.
You can see the format of the response in the examples.
**Note:** The following endpoints have special rate limit requirements that
Expand All @@ -10001,11 +10001,11 @@ apiDescription: >
can be made per 60 seconds.
* Only 5 requests to any and all `v2/cdn/endpoints` can be made per 10
seconds. This includes `v2/cdn/endpoints`,
seconds. This includes `v2/cdn/endpoints`,
`v2/cdn/endpoints/$ENDPOINT_ID`, and `v2/cdn/endpoints/$ENDPOINT_ID/cache`.
* Only 50 strings within the `files` json struct in the
`v2/cdn/endpoints/$ENDPOINT_ID/cache`
[payload](https://docs.digitalocean.com/reference/api/api-reference/#operation/cdn_purge_cache)
[payload](https://docs.digitalocean.com/reference/api/api-reference/#operation/cdn_purge_cache)
can be requested every 20 seconds.
### Sample Rate Limit Headers
Expand Down Expand Up @@ -10051,7 +10051,7 @@ apiDescription: >
`curl` command. This will allow us to demonstrate the various endpoints in a
simple, textual format.
These examples assume that you are using a Linux or macOS command line. To run
these commands on a Windows machine, you can either use cmd.exe, PowerShell,
or WSL:
Expand All @@ -10076,7 +10076,7 @@ apiDescription: >
a Windows machine.
Install WSL with our [community
tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-the-windows-subsystem-for-linux-2-on-microsoft-windows-10),
tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-the-windows-subsystem-for-linux-2-on-microsoft-windows-10),
then follow this API documentation normally.
Expand Down
Loading

0 comments on commit 9f96eea

Please sign in to comment.