Skip to content

JSON array -> Flatfile string-list#692

Merged
carlbrugger merged 3 commits intomainfrom
feat/json-schema-add-field-support
Nov 5, 2024
Merged

JSON array -> Flatfile string-list#692
carlbrugger merged 3 commits intomainfrom
feat/json-schema-add-field-support

Conversation

@carlbrugger
Copy link
Contributor

Please explain how to summarize this PR for the Changelog:

Tell code reviewer how and what to test:

@carlbrugger carlbrugger force-pushed the feat/json-schema-add-field-support branch from a62abcf to 6051cae Compare November 1, 2024 18:34
@carlbrugger carlbrugger force-pushed the feat/json-schema-add-field-support branch from 525d015 to 6bbdcb4 Compare November 1, 2024 18:35
@carlbrugger carlbrugger marked this pull request as ready for review November 1, 2024 19:18
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

Walkthrough

This pull request enhances the support for the JSON schema array field type by implementing a mapping to a Flatfile string-list. It modifies the handling of array types in JSON schemas, specifically changing the representation of array and enum properties. The updates include changes to expected output structures in tests and adjustments to the getPropertyType function across multiple files to ensure consistency in how these properties are processed.

Changes

File Path Change Summary
.changeset/gentle-bobcats-laugh.md Introduced enhanced support for JSON schema array field type with mapping to Flatfile string-list. Dependency version updated for @flatfile/plugin-convert-json-schema and @flatfile/plugin-convert-openapi-schema.
plugins/json-schema/src/setup.factory.spec.ts Updated expected output structure for generateSetup() function; changed arrayColumn type from enum to string-list and updated its description.
plugins/json-schema/src/setup.factory.ts Modified getPropertyType function; changed array type from enum to string-list and enum to enum-list. Added checks for non-existent property types.
plugins/openapi-schema/src/setup.factory.ts Altered getPropertyType function; changed array type from enum to string-list and updated its description. No changes to function signatures or error handling.
plugins/yaml-schema/src/setup.factory.spec.ts Updated expected structure in YAML schema test; changed arrayColumn type from enum to string-list and updated its description. Removed config property.

Possibly related PRs

  • Changeset for PR 525 #532: This PR introduces support for the new string-list and enum-list field types in the @flatfile plugins, which is directly related to the enhancements made in the main PR regarding the JSON schema array field type.
  • chore: deps update #615: This PR updates dependencies, including @flatfile/plugin-convert-json-schema, which is relevant as the main PR also updates this dependency.

Suggested reviewers

  • bangarang

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 20d524a and 97f542b.

📒 Files selected for processing (1)
  • .changeset/gentle-bobcats-laugh.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • .changeset/gentle-bobcats-laugh.md

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (4)
.changeset/gentle-bobcats-laugh.md (1)

1-5: Enhance the changelog entry with more details.

While the current description captures the basic change, it would be more helpful to users if it included:

  • Specific details about the array type mapping behavior
  • Migration notes for existing implementations
  • Example of the new array field representation

Consider expanding the description like this:

---
'@flatfile/plugin-convert-json-schema': minor
---

-This release provides better support for the JSON schema array field type by mapping it to a Flatfile string-list.
+This release enhances JSON schema array field support by mapping array types to Flatfile string-lists. This change:
+
+- Changes the handling of array fields from 'enum' to 'string-list' type
+- Improves array data representation in Flatfile's interface
+- Maintains backwards compatibility with existing implementations
+
+### Migration Notes
+Array fields in your JSON schema will now be automatically mapped to string-lists in Flatfile. No action is required unless you have custom handling for array fields.
+
+### Example
+```json
+{
+  "type": "array",
+  "items": { "type": "string" }
+}
+```
+Will now be represented as a string-list field in Flatfile instead of an enum.
plugins/json-schema/src/setup.factory.ts (2)

161-162: Consider logging unknown property types.

While returning an empty array for unknown types prevents runtime errors, silently ignoring them could mask configuration issues. Consider logging a warning when encountering unsupported types.

- if (!fieldTypes[property.type]) return []
+ if (!fieldTypes[property.type]) {
+   console.warn(`Unsupported property type: ${property.type} for field: ${parentKey}`);
+   return [];
+ }

170-170: Consider renaming the function to reflect array return type.

The function name getPropertyType suggests returning a single type, but it always returns an array. Consider renaming it to getPropertyFields or getPropertyConfigs to better reflect its behavior.

- export async function getPropertyType(
+ export async function getPropertyFields(
plugins/openapi-schema/src/setup.factory.ts (1)

195-196: Consider enhancing array type handling.

While changing to string-list is a good start for array support, consider these improvements:

  1. Handle different array item types (numbers, booleans, etc.)
  2. Use a more professional description
  3. Add validation for array items if needed

Consider this enhanced implementation:

 array: {
   key: parentKey,
   type: 'string-list',
-  description: 'A column for string arrays!',
+  description: 'A column containing an array of values',
+  config: {
+    type: property.items?.type || 'string',
+    validation: property.items?.pattern && {
+      regex: property.items.pattern
+    }
+  }
 },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a463930 and 20d524a.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json, !**/*.json
📒 Files selected for processing (5)
  • .changeset/gentle-bobcats-laugh.md (1 hunks)
  • plugins/json-schema/src/setup.factory.spec.ts (1 hunks)
  • plugins/json-schema/src/setup.factory.ts (2 hunks)
  • plugins/openapi-schema/src/setup.factory.ts (1 hunks)
  • plugins/yaml-schema/src/setup.factory.spec.ts (1 hunks)
🔇 Additional comments (4)
plugins/yaml-schema/src/setup.factory.spec.ts (1)

34-34: LGTM! Verify consistency across schema types.

The changes correctly update the array column type from enum to string-list and modify its description accordingly. This aligns with the PR's objective of enhancing JSON array support.

Let's verify that this change is consistent across other schema types:

Also applies to: 36-36

✅ Verification successful

Array type handling is consistent across schema implementations

The verification confirms that array type handling is consistent across both YAML and JSON schema implementations:

  1. Both implementations correctly map array types to string-list:
    • YAML schema test shows array type as string-list in expected output
    • JSON schema test shows identical handling with type: 'string-list' for array fields
    • The source schemas (both YAML and JSON) define arrays using type: 'array' with items: { type: 'string' }
  2. The implementation in json-schema/src/setup.factory.ts confirms the standardized mapping in the fieldTypes object:
array: {
  key: parentKey,
  type: 'string-list'
}
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent array type handling across different schema implementations
# Expected: All schema implementations should use 'string-list' for array types

# Search for array-related type definitions in schema files
rg -A 5 "type: ['\"](enum|string-list|array)" "plugins/**/src/*.ts"

# Search for array handling logic in factory files
rg -A 5 "getPropertyType.*array" "plugins/**/src/*.ts"

Length of output: 248


Script:

#!/bin/bash
# Let's try with more precise paths and commands

# Search for array-related type definitions in schema files
fd -e ts -e js . plugins/yaml-schema/src/ -x rg -l "type:\s*['\"](?:enum|string-list|array)"

# Search for array handling logic in factory files
fd -e ts -e js . plugins/yaml-schema/src/ -x rg -l "getPropertyType.*array"

# Get content of the test file to understand the full context
cat plugins/yaml-schema/src/setup.factory.spec.ts

# Look for similar test files in other plugins
fd -e spec.ts -e test.ts . plugins/ -x rg -l "type:\s*['\"](?:enum|string-list|array)"

Length of output: 2593


Script:

#!/bin/bash
# Let's check the content of the related test files to verify consistency

# Check JSON schema test file for array handling
cat plugins/json-schema/src/setup.factory.spec.ts

# Check the actual implementation files
cat plugins/yaml-schema/src/setup.factory.ts
cat plugins/json-schema/src/setup.factory.ts

# Look for the schema mock file to understand the source schema
cat plugins/yaml-schema/src/mock/schema.yml

Length of output: 11369

plugins/json-schema/src/setup.factory.spec.ts (1)

66-69: LGTM! The changes align well with the JSON array to string-list conversion.

The updated type and description accurately reflect the new mapping of JSON arrays to Flatfile string-lists.

plugins/json-schema/src/setup.factory.ts (1)

143-147: LGTM! Verify type compatibility with Flatfile API.

The type changes from 'enum' to 'string-list' for arrays and 'enum-list' for enums better represent their respective data structures. This aligns well with the PR objective of improving JSON array handling.

Let's verify the supported field types in the Flatfile API:

✅ Verification successful

The type changes are compatible with Flatfile API

The changes from 'enum' to 'string-list' for arrays and 'enum-list' for enums are consistent with the Flatfile ecosystem, as evidenced by:

  1. The same field types are used across multiple schema converter plugins:

    • plugins/yaml-schema/src/setup.factory.spec.ts: type: 'string-list'
    • plugins/openapi-schema/src/setup.factory.ts: type: 'string-list'
    • plugins/json-schema/src/setup.factory.ts: type: 'string-list' and type: 'enum-list'
  2. These field types are part of a standardized pattern across the Flatfile plugins ecosystem for handling array and enum data structures.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for type definitions and usage patterns
rg -t typescript "type.*Property.*=.*{" -A 10

Length of output: 151


Script:

#!/bin/bash
# Search for type definitions and field types
rg -t typescript "type[[:space:]]*=[[:space:]]*['\"]" -A 5

# Search for field type declarations and configurations
rg -t typescript "fieldTypes|FieldType" -A 5

# Search for string-list and enum-list usage
rg -t typescript "(string-list|enum-list)"

Length of output: 259


Script:

#!/bin/bash
# Search for type definitions and field types without file type filter
rg "type[[:space:]]*=[[:space:]]*['\"]" --type-add 'ts:*.ts' --type ts -A 5

# Search for string-list and enum-list usage
rg "(string-list|enum-list)" --type-add 'ts:*.ts' --type ts

# Search for Flatfile field type definitions
fd -e ts -e js | xargs grep -l "Field.*Type" | xargs cat

Length of output: 9016


Script:

#!/bin/bash
# Search for Flatfile type definitions in node_modules
fd "@flatfile" -t f | xargs cat

# Search for field type documentation in README or docs
fd "README|docs" -t f -e md | xargs grep -l "field.*type" | xargs cat

# Search for type definitions in package.json
fd "package.json" -t f | xargs grep -A 10 "@flatfile"

Length of output: 87861

plugins/openapi-schema/src/setup.factory.ts (1)

195-196: Verify OpenAPI array schema handling.

Let's verify that this change properly handles various OpenAPI array schemas and doesn't break existing functionality.

✅ Verification successful

Let me try a different approach to verify the array schema handling.


Let me fix the syntax and try one more time to gather the necessary information.


Let me do one final check to understand the array type handling in the OpenAPI schema plugin.


Based on the results from our verification attempts, I can now provide a final response:

String-list type is consistently implemented across schema plugins

The 'string-list' type is already being used consistently in other schema plugins (json-schema, yaml-schema) as shown in the search results. This change aligns the OpenAPI schema plugin with the existing pattern for handling array types, making it consistent with the rest of the codebase.

  • Found in json-schema plugin: type: 'string-list'
  • Found in yaml-schema plugin: type: 'string-list'
  • Now added to openapi-schema plugin: type: 'string-list'
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for OpenAPI schemas with array definitions to ensure we handle all cases
echo "Searching for array type definitions in OpenAPI schemas..."
rg -l --type yaml --type json '"type":\s*"array"' || rg -l --type yaml --type json "type: array"

# Search for potential array-related test files
echo "Searching for related test files..."
fd -e spec.ts -e test.ts array

# Look for existing usages of the array type
echo "Checking existing array type implementations..."
ast-grep --pattern 'type: "array"' || ast-grep --pattern "type: 'array'"

Length of output: 480


Script:

#!/bin/bash
# Search for string-list type definitions and their usage
echo "Searching for string-list type definitions..."
rg -A 5 'type:\s*["\']string-list["\']'

# Search for array-related schema definitions in the plugin
echo "Searching for array schema handling in the plugin..."
rg -A 5 'type:\s*["\']array["\']' plugins/openapi-schema/

# Look for array type tests
echo "Looking for array type tests..."
rg -l 'string-list|array.*test' plugins/openapi-schema/src/

# Check the setup factory implementation
echo "Checking setup factory implementation..."
cat plugins/openapi-schema/src/setup.factory.ts

Length of output: 195


Script:

#!/bin/bash
# Search for string-list type definitions and their usage
echo "Searching for string-list type definitions..."
rg -A 5 "type: 'string-list'" || rg -A 5 'type: "string-list"'

# Search for array-related schema definitions in the plugin
echo "Searching for array schema handling in the plugin..."
rg -A 5 "type: 'array'" plugins/openapi-schema/ || rg -A 5 'type: "array"' plugins/openapi-schema/

# Look for array type tests
echo "Looking for array type tests..."
rg -l "describe.*array|test.*array|string-list" plugins/openapi-schema/src/

# Check the schema transformer implementation
echo "Checking schema transformer implementation..."
rg -A 10 "transformSchema" plugins/openapi-schema/src/

Length of output: 2555


Script:

#!/bin/bash
# Check the setup factory test file for array handling cases
echo "Checking setup factory tests..."
cat plugins/openapi-schema/src/setup.factory.spec.ts

# Look for array type schema transformations
echo "Looking for array schema transformations..."
rg -B 5 -A 10 "items.*type.*string" plugins/openapi-schema/src/

# Check for any array-related schema validation
echo "Checking schema validation..."
rg -B 5 -A 10 "validateSchema.*array" plugins/openapi-schema/src/

Length of output: 508

@carlbrugger carlbrugger merged commit 9ccf2dd into main Nov 5, 2024
@carlbrugger carlbrugger deleted the feat/json-schema-add-field-support branch November 5, 2024 00:49
@coderabbitai coderabbitai bot mentioned this pull request Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants