Skip to content

Commit

Permalink
chore: remove transformations introduced for openapi 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed Aug 7, 2023
1 parent 53e063f commit 80dd57f
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 133 deletions.
2 changes: 1 addition & 1 deletion json-schema.json

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -9472,11 +9472,9 @@
},
{
"type": "boolean"
},
{
"type": "null"
}
],
"nullable": true,
"description": "Content of the JSON metadata"
}
},
Expand Down Expand Up @@ -10890,11 +10888,9 @@
},
{
"type": "boolean"
},
{
"type": "null"
}
],
"nullable": true,
"description": "JSON contents of the `timelock` script, null for `plutus` scripts"
}
},
Expand Down
4 changes: 2 additions & 2 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6953,7 +6953,7 @@ components:
- type: integer
- type: number
- type: boolean
- type: 'null'
nullable: true
description: Content of the JSON metadata
required:
- tx_hash
Expand Down Expand Up @@ -7975,7 +7975,7 @@ components:
- type: integer
- type: number
- type: boolean
- type: 'null'
nullable: true
description: JSON contents of the `timelock` script, null for `plutus` scripts
required:
- json
Expand Down
101 changes: 6 additions & 95 deletions src/functions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,89 +15,6 @@ const file = fs.readFileSync(
);
const spec = YAML.parse(file);

export const transformSchemaElement = (schema: any): any => {
// Note: This is mostly for OpenAPI 3.1 definitions which we stopped using due to too much incompatibility with 3rd party tools
// To generate response schema supported by fast-json-stringify,
// we need to convert array type (["null", "<other type>"]) to type: "<other type>" with nullable set to true.
// Note: Alternative approach for values with multiple types is to use anyOf/oneOf.
// https://github.com/fastify/fast-json-stringify#anyof-and-oneof

if (schema.type === 'object' && schema.properties) {
// convert type in object properties
for (const propertyKey of Object.keys(schema.properties)) {
const property = schema.properties[propertyKey];
if (
property.type === 'object' &&
property.additionalProperties === true &&
!property.properties
) {
if (!property.anyOf && !property.oneOf) {
// Workaround for fast-json-stringify
// If object's property is arbitrary object,
// convert {type: 'object', additionalProperties: true} to {}
delete schema.properties[propertyKey].type;
delete schema.properties[propertyKey].additionalProperties;
}
}
if (property.anyOf) {
if (
property.anyOf.find(
(p: unknown) =>
typeof p === 'object' &&
p !== null &&
'type' in p &&
p.type === 'null',
)
) {
// if array of anyOf items includes {"type": "null"} then set nullable to true on the parent
property.nullable = true;
}
}
schema.properties[propertyKey] = transformSchemaElement(
schema.properties[propertyKey],
);
}
return schema;
} else if (schema.type === 'array' && schema.items) {
// convert type in array items
schema.items = transformSchemaElement(schema.items);
return schema;
} else if (Array.isArray(schema.type)) {
const isNullable = schema.type.includes('null');
if (isNullable) {
if (schema.type.length > 2) {
throw Error(
`Error in ${JSON.stringify(
schema,
)}. Type doesn't support an array with multiple values. Use anyOf/oneOf.`,
);
}

return transformSchemaElement({
...schema,
type: schema.type.filter((a: string) => a !== 'null')[0],
nullable: true,
});
} else {
// edge case where type is an array with only 1 element
if (schema.type.length === 1) {
return {
...schema,
type: schema.type[0],
};
}
throw Error(
`Error in ${JSON.stringify(
schema,
)}. Type doesn't support an array with multiple values. Use anyOf/oneOf.`,
);
}
} else {
// do nothing
return schema;
}
};

export const getSchemaForEndpoint = (endpointName: string) => {
if (!spec.paths[endpointName]) {
throw Error(
Expand Down Expand Up @@ -143,24 +60,20 @@ export const getSchemaForEndpoint = (endpointName: string) => {
);

if (schemaReferenceOrValue.type) {
responses.response[200] = transformSchemaElement({
responses.response[200] = {
...schemaReferenceOrValue,
items: spec.components.schemas[nestedSchemaName],
});
};
} else {
responses.response[200] = transformSchemaElement(
spec.components.schemas[nestedSchemaName],
);
responses.response[200] = spec.components.schemas[nestedSchemaName];
}
} else {
// is not nested reference
responses.response[200] = transformSchemaElement(
spec.components.schemas[schemaName],
);
responses.response[200] = spec.components.schemas[schemaName];
}
} else {
// is not reference
responses.response[200] = transformSchemaElement(referenceOrValue);
responses.response[200] = referenceOrValue;
}

// anyOf case
Expand All @@ -173,9 +86,7 @@ export const getSchemaForEndpoint = (endpointName: string) => {
'',
);

const item = transformSchemaElement(
spec.components.schemas[schemaName],
);
const item = spec.components.schemas[schemaName];
anyOfResult['anyOf'].push(item);
}

Expand Down
8 changes: 4 additions & 4 deletions src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5642,11 +5642,11 @@ export interface components {
/** @description Transaction hash that contains the specific metadata */
tx_hash: string;
/** @description Content of the JSON metadata */
json_metadata: ({
json_metadata: (({
[key: string]: unknown | undefined;
}) & (string | ({
[key: string]: unknown | undefined;
}) | (Record<string, never>)[] | number | number | boolean | null);
}) | (Record<string, never>)[] | number | number | boolean)) | null;
})[];
/**
* @example [
Expand Down Expand Up @@ -6575,11 +6575,11 @@ export interface components {
*/
script_json: {
/** @description JSON contents of the `timelock` script, null for `plutus` scripts */
json: ({
json: (({
[key: string]: unknown | undefined;
}) & (string | ({
[key: string]: unknown | undefined;
}) | (Record<string, never>)[] | number | number | boolean | null);
}) | (Record<string, never>)[] | number | number | boolean)) | null;
};
/**
* @example {
Expand Down
13 changes: 10 additions & 3 deletions test/tests/__snapshots__/get-schema-for-endpoint.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4406,11 +4406,13 @@ Testnet: https://github.com/input-output-hk/metadata-registry-testnet/
"type": "integer",
},
"onchain_metadata": {
"additionalProperties": true,
"description": "On-chain metadata which SHOULD adhere to the valid standards,
based on which we perform the look up and display the asset
(best effort)
",
"nullable": true,
"type": "object",
},
"onchain_metadata_extra": {
"description": "Arbitrary plutus data (CIP68).
Expand Down Expand Up @@ -7837,6 +7839,7 @@ under which it is valid
"type": "integer",
},
"cost_models": {
"additionalProperties": true,
"description": "Cost models parameters for Plutus Core scripts",
"example": {
"PlutusV1": {
Expand All @@ -7849,6 +7852,7 @@ under which it is valid
},
},
"nullable": true,
"type": "object",
},
"decentralisation_param": {
"description": "Percentage of blocks produced by federated nodes",
Expand Down Expand Up @@ -9011,6 +9015,7 @@ under which it is valid
"type": "integer",
},
"cost_models": {
"additionalProperties": true,
"description": "Cost models parameters for Plutus Core scripts",
"example": {
"PlutusV1": {
Expand All @@ -9023,6 +9028,7 @@ under which it is valid
},
},
"nullable": true,
"type": "object",
},
"decentralisation_param": {
"description": "Percentage of blocks produced by federated nodes",
Expand Down Expand Up @@ -12558,9 +12564,6 @@ State \`gc\` means that a previously \`unpinned\` item has been garbage collecte
{
"type": "boolean",
},
{
"type": "null",
},
],
"description": "Content of the JSON metadata",
"nullable": true,
Expand Down Expand Up @@ -13988,8 +13991,10 @@ relative to the start of the network
"type": "string",
},
"metadata": {
"additionalProperties": true,
"description": "The cached metadata of the \`metadata_url\` file.",
"nullable": true,
"type": "object",
},
"metadata_hash": {
"description": "Hash of the metadata file",
Expand Down Expand Up @@ -17036,7 +17041,9 @@ relative to the start of the network
},
"properties": {
"json_value": {
"additionalProperties": true,
"description": "JSON content of the datum",
"type": "object",
},
},
"required": [
Expand Down
5 changes: 2 additions & 3 deletions test/tests/get-schema-for-endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,9 @@ describe('getSchemaForEndpoint', () => {
type: 'object',
properties: {
json_value: {
additionalProperties: true,
description: 'JSON content of the datum',
type: 'object',
},
},
required: ['json_value'],
Expand Down Expand Up @@ -1478,9 +1480,6 @@ describe('getSchemaForEndpoint', () => {
{
"type": "boolean",
},
{
"type": "null",
},
],
"description": "Content of the JSON metadata",
"nullable": true,
Expand Down
19 changes: 0 additions & 19 deletions test/tests/get-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,4 @@ describe('getSchema', () => {
});
});

fixtures.transformSchemaElement.map(fixture => {
test(`transformSchemaElement: ${fixture.description}`, async () => {
expect(transformSchemaElement(fixture.data)).toStrictEqual(
fixture.result,
);
});
});
fixtures.transformSchemaElementError.map(fixture => {
test(`transformSchemaElement: ${fixture.description}`, async () => {
expect(() => transformSchemaElement(fixture.data)).toThrowError(
fixture.result,
);
});
test(`transformSchemaElement: ${fixture.description}`, async () => {
expect(() => transformSchemaElement(fixture.data)).toThrowError(
fixture.result,
);
});
});
});

0 comments on commit 80dd57f

Please sign in to comment.