Skip to content

Commit

Permalink
fix: rename namespace to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
qwelias committed Jul 28, 2022
1 parent cf8d693 commit 89da88c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
12 changes: 6 additions & 6 deletions generator/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function normalizeSchema<T extends JSONSchema7>(originalSchema: T, rootId: strin
// @fastify/swagger just deletes `definitions` https://github.com/fastify/fastify-swagger/blob/master/lib/spec/swagger/utils.js#L325
// so we rename it to `namespace`
rootId = `${rootId}#/definitions/`;
newRootId = `${newRootId}#/namespace/`;
newRootId = `${newRootId}#/properties/`;

const escapeGenerics = (key: string) => key.replace(/</gim, '__').replace(/>/gim, '__');

Expand Down Expand Up @@ -76,7 +76,7 @@ function normalizeSchema<T extends JSONSchema7>(originalSchema: T, rootId: strin
});

const { definitions, ...rest } = mergedSchema;
return { namespace: definitions, ...rest };
return { properties: definitions, ...rest };
}
export default async (params: { files: string[] }) => {
const compilerOptions: TsConfigJson['compilerOptions'] = {
Expand Down Expand Up @@ -126,13 +126,13 @@ export default async (params: { files: string[] }) => {
const symbols = generator?.getMainFileSymbols(program, [file]) ?? [];

const jsonschema = generator?.getSchemaForSymbols(symbols, true);
const { namespace = {}, $schema } = normalizeSchema(jsonschema as JSONSchema7, PLACEHOLDER_ID, name);
const { properties = {}, $schema } = normalizeSchema(jsonschema as JSONSchema7, PLACEHOLDER_ID, name);

console.log('Generating', symbols, 'for', file);

const results: Record<string, { request: Object; response: Object }> = {};

for (const [defName, def] of Object.entries(namespace)) {
for (const [defName, def] of Object.entries(properties)) {
if (typeof def === 'boolean') continue;
if (!def?.properties) continue;
if (typeof def?.properties.paths === 'boolean') continue;
Expand All @@ -141,7 +141,7 @@ export default async (params: { files: string[] }) => {
if (!tag || typeof tag === 'boolean') continue;

if (tag.enum?.length === 1 && tag.enum?.[0] === 'BETTER-FASTIFY-SCHEMA') {
delete namespace[defName];
delete properties[defName];
}

const paths = Object.entries(def.properties.paths?.properties ?? {});
Expand Down Expand Up @@ -175,7 +175,7 @@ export default async (params: { files: string[] }) => {
const existing = savedExists ? await fs.readFile(saved, { encoding: 'utf8' }).catch(() => {}) : '';
const newContents = JSON.stringify(
{
schema: { namespace, $id: name, $schema },
schema: { properties, $id: name, $schema, type: 'object' },
fastify: results,
$hash: hash,
},
Expand Down
24 changes: 12 additions & 12 deletions tap-snapshots/test/integration.test.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Error: body/user/name must be string {
"params": Object {
"type": "string",
},
"schemaPath": "test_schema#/namespace/User/properties/name/type",
"schemaPath": "test_schema#/properties/User/properties/name/type",
},
],
"validationContext": "body",
Expand Down Expand Up @@ -121,7 +121,7 @@ Object {
"body": Object {
"properties": Object {
"user": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
"required": Array [
Expand Down Expand Up @@ -203,7 +203,7 @@ Object {
"body": Object {
"properties": Object {
"user": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
"required": Array [
Expand Down Expand Up @@ -286,7 +286,7 @@ Object {
"body": Object {
"properties": Object {
"user": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
"required": Array [
Expand Down Expand Up @@ -432,7 +432,7 @@ Object {
"frame": Object {
"allOf": Array [
Object {
"$ref": "test_schema#/namespace/Omit__Obj,\\"type\\"__",
"$ref": "test_schema#/properties/Omit__Obj,\\"type\\"__",
},
Object {
"properties": Object {
Expand Down Expand Up @@ -525,7 +525,7 @@ Object {
},
"response": Object {
"200": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
},
Expand Down Expand Up @@ -587,7 +587,7 @@ Object {
},
"response": Object {
"200": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
},
Expand Down Expand Up @@ -678,7 +678,7 @@ Object {
},
"response": Object {
"200": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
},
Expand Down Expand Up @@ -797,7 +797,7 @@ Object {
},
"response": Object {
"200": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
},
Expand Down Expand Up @@ -840,7 +840,7 @@ Object {
},
"response": Object {
"200": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
},
Expand Down Expand Up @@ -939,7 +939,7 @@ Object {
},
"response": Object {
"200": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
},
Expand Down Expand Up @@ -983,7 +983,7 @@ Object {
},
"response": Object {
"200": Object {
"$ref": "test_schema#/namespace/User",
"$ref": "test_schema#/properties/User",
},
},
},
Expand Down
17 changes: 9 additions & 8 deletions test/test_schema.gen.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schema": {
"namespace": {
"properties": {
"SharedRequest": {
"type": "object",
"required": [
Expand Down Expand Up @@ -48,7 +48,7 @@
"type": "object",
"allOf": [
{
"$ref": "test_schema#/namespace/Omit__Obj,\"type\"__"
"$ref": "test_schema#/properties/Omit__Obj,\"type\"__"
},
{
"type": "object",
Expand Down Expand Up @@ -80,12 +80,13 @@
}
},
"$id": "test_schema",
"$schema": "http://json-schema.org/draft-07/schema#"
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object"
},
"fastify": {
"GET /": {
"request": {
"$ref": "test_schema#/namespace/SharedRequest",
"$ref": "test_schema#/properties/SharedRequest",
"type": "object",
"required": [
"headers",
Expand Down Expand Up @@ -119,7 +120,7 @@
},
"response": {
"200": {
"$ref": "test_schema#/namespace/User"
"$ref": "test_schema#/properties/User"
}
}
},
Expand All @@ -137,7 +138,7 @@
],
"properties": {
"user": {
"$ref": "test_schema#/namespace/User"
"$ref": "test_schema#/properties/User"
}
}
}
Expand All @@ -152,7 +153,7 @@
],
"properties": {
"user": {
"$ref": "test_schema#/namespace/User"
"$ref": "test_schema#/properties/User"
},
"msg": {
"type": "string"
Expand Down Expand Up @@ -208,7 +209,7 @@
"type": "object",
"allOf": [
{
"$ref": "test_schema#/namespace/Omit__Obj,\"type\"__"
"$ref": "test_schema#/properties/Omit__Obj,\"type\"__"
},
{
"type": "object",
Expand Down

0 comments on commit 89da88c

Please sign in to comment.