Skip to content
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

Allow ignoring single fields/errors #1053

Closed
Bluscream opened this issue Sep 3, 2024 · 2 comments
Closed

Allow ignoring single fields/errors #1053

Bluscream opened this issue Sep 3, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Bluscream
Copy link

You have already researched for similar issues?

I hope so

What are you trying to achieve, or the steps to reproduce?

I want to merge all schemas from different API endpoint responses so i can let AI build a OpenAPI spec file for me

import fs from 'fs/promises';
import path from 'path';
import { defaultResolver, mergeSchemas } from '@fastify/merge-json-schemas';

async function getAllJsonFiles(directory) {
  const files = await fs.readdir(directory);
  const jsonFiles = [];

  for (const file of files) {
    const filePath = path.join(directory, file);
    const stats = await fs.stat(filePath);

    if (stats.isDirectory()) {
      jsonFiles.push(...await getAllJsonFiles(filePath));
    } else if (path.extname(file) === '.json') {
      jsonFiles.push(filePath);
    }
  }

  return jsonFiles;
}

async function loadSchema(file) {
  try {
    const content = await fs.readFile(file, 'utf8');
    return JSON.parse(content);
  } catch (error) {
    console.error(`Error loading schema from ${file}:`, error);
    throw error;
  }
}

async function mergeSchemasWithFallback(files) {
  let schemas = [];

  for (const file of files) {
    try {
      const schema = await loadSchema(file);
      
      // Check if the schema has a custom resolver
      if (schema.$ref && !schema.$ref.startsWith('#')) {
        console.log(`Found external reference: ${schema.$ref}`);
        
        // Try to resolve the $ref
        let resolvedRef;
        try {
          resolvedRef = await fs.promises.readFile(schema.$ref, 'utf8');
          resolvedRef = JSON.parse(resolvedRef);
        } catch (error) {
          console.error(`Failed to resolve reference: ${schema.$ref}`, error);
        }
        
        // Merge the resolved schema
        schemas.push(resolvedRef || schema);
      } else {
        schemas.push(schema);
      }
    } catch (error) {
      console.error(`Error processing schema from ${file}:`, error);
    }
  }

  try {
    return mergeSchemas(schemas, { onConflict: "first"});
  } catch (error) {
    console.error('An error occurred during schema merging:', error);
    throw error;
  }
}

async function writeMergedSchemaToFile(mergedSchema) {
  try {
    await fs.writeFile('merged.schema.json', JSON.stringify(mergedSchema, null, 2));
    console.log('Merged schema has been written to merged.schema.json');
  } catch (error) {
    console.error('Error writing merged schema to file:', error);
  }
}

(async () => {
  try {
    const jsonFiles = await getAllJsonFiles(process.cwd());
    
    const mergedSchema = await mergeSchemasWithFallback(jsonFiles);
    
    // Write the merged schema to a file
    await writeMergedSchemaToFile(mergedSchema);

    console.log(JSON.stringify(mergedSchema, null, 2));

  } catch (error) {
    console.error('An error occurred:', error);
  }
})();

What was the result you received?

PS C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\schemas\pietsmiet.de> node ..\..\scripts\merge-schemas.js
An error occurred during schema merging: MergeError [JsonSchemaMergeError]: Failed to merge "type" keyword schemas.
    at hybridArraysIntersection (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\lib\resolvers.js:33:11)
    at _mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:310:5)
    at mergeProperties (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:208:34)
    at _mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:310:5)
    at mergeObjects (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:228:34)
    at _mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:310:5)
    at mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:353:24)
    at mergeSchemasWithFallback (file:///C:/Users/blusc/AppData/Local/Temp/grayjay-source-pietsmietde/scripts/merge-schemas.js:64:12)
    at async file:///C:/Users/blusc/AppData/Local/Temp/grayjay-source-pietsmietde/scripts/merge-schemas.js:84:26 {
  code: 'JSON_SCHEMA_MERGE_ERROR',
  schemas: [ [ 'null' ], [ 'null' ], [ 'string' ], [ 'string' ], [ 'string' ] ]
}
An error occurred: MergeError [JsonSchemaMergeError]: Failed to merge "type" keyword schemas.
    at hybridArraysIntersection (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\lib\resolvers.js:33:11)
    at _mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:310:5)
    at mergeProperties (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:208:34)
    at _mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:310:5)
    at mergeObjects (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:228:34)
    at _mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:310:5)
    at mergeSchemas (C:\Users\blusc\AppData\Local\Temp\grayjay-source-pietsmietde\node_modules\@fastify\merge-json-schemas\index.js:353:24)
    at mergeSchemasWithFallback (file:///C:/Users/blusc/AppData/Local/Temp/grayjay-source-pietsmietde/scripts/merge-schemas.js:64:12)
    at async file:///C:/Users/blusc/AppData/Local/Temp/grayjay-source-pietsmietde/scripts/merge-schemas.js:84:26 {
  code: 'JSON_SCHEMA_MERGE_ERROR',
  schemas: [ [ 'null' ], [ 'null' ], [ 'string' ], [ 'string' ], [ 'string' ] ]
}

What did you expect?

a merged.schema.json file in my directory

Context

  • node version: v22.6.0
  • fastify version: "@fastify/merge-json-schemas": "github:fastify/merge-json-schemas"
  • os: Windows
  • any other relevant information:

Please read this entire template before posting any issue. If you ignore these instructions
and post an issue here that does not follow the instructions, your issue might be closed,
locked, and assigned the missing discussion label.

@dosubot dosubot bot added the bug Something isn't working label Sep 3, 2024
@mcollina
Copy link
Member

mcollina commented Sep 3, 2024

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

@Bluscream
Copy link
Author

I worked around this problem by generating a whole OpenAPI spec from scratch using a HAR file, which allowed me to extract all response schemas into one file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants