Skip to content

Conversation

versona-tech
Copy link

@versona-tech versona-tech commented Sep 20, 2025

  • Enhanced handleNone function to intelligently guess content-type from headers and data structure
  • Added support for detecting JSON, URL-encoded, multipart form data, and text content types
  • Checks Content-Type header (case-insensitive) and delegates to appropriate handlers
  • Implements heuristic content-type detection based on data format when headers are not explicit
  • Improves API reliability by automatically choosing correct parsing strategy
  • Addresses FIXME comment requesting content-type guessing functionality

Resolves: SmythOS/sre#TODO-content-type-detection

📝 Description

🚀 Improve Content-Type Detection in API Call Parser

📝 Description

This PR enhances the handleNone function in parseData.ts to intelligently detect and handle different content types when none is explicitly specified. The improvement addresses a long-standing FIXME comment and significantly improves API reliability.

🎯 What This Fixes

Before: The handleNone function would simply return raw data without attempting to determine the appropriate content type, leading to potential parsing issues.

After: The function now:

  1. ✅ Checks for explicit Content-Type headers (case-insensitive)
  2. ✅ Intelligently delegates to appropriate handlers based on detected content type
  3. ✅ Implements heuristic detection for JSON, URL-encoded, text, and other formats
  4. ✅ Maintains backward compatibility with fallback behavior

🔧 Changes Made

Enhanced Content-Type Detection Logic:

  • Header Analysis: Checks content-type and Content-Type headers
  • JSON Detection: Identifies JSON structures using bracket/brace patterns + validation
  • URL-Encoded Detection: Recognizes form-encoded data patterns
  • Text Detection: Falls back to text handling for string data
  • Object Handling: Automatically stringifies objects for JSON processing

Code Quality Improvements:

  • ✅ Resolves FIXME comment: "try to guess the content type from headers content-type and data"
  • ✅ Maintains existing API compatibility
  • ✅ Comprehensive error handling with graceful fallbacks
  • ✅ Clean, readable implementation with clear logic flow

🧪 Testing

  • ✅ Project builds successfully without errors
  • ✅ All existing functionality preserved
  • ✅ CLI tools continue to work as expected
  • ✅ No breaking changes introduced

📋 Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Functionality tested locally
  • No breaking changes
  • Commit message follows conventional format
  • DCO sign-off included (git commit -s)
  • FIXME comment addressed and resolved

🎉 Impact

This improvement enhances the robustness of API calls within the SmythOS platform by:

  • Reducing parsing errors through intelligent content-type detection
  • Improving developer experience with more predictable behavior
  • Maintaining compatibility while adding new capabilities
  • Following best practices for content-type handling

Type: fix - Bug fix and improvement
Scope: core/APICall - Core API parsing functionality
Breaking Change: ❌ No

🔗 Related Issues

  • Fixes #
  • Relates to #

🔧 Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 📚 Documentation update
  • 🔧 Code refactoring (no functional changes)
  • 🧪 Test improvements
  • 🔨 Build/CI changes

✅ Checklist

  • Self-review performed
  • Tests added/updated
  • Documentation updated (if needed)

versona-tech added 2 commits September 20, 2025 07:52
- Enhanced handleNone function to intelligently guess content-type from headers and data structure
- Added support for detecting JSON, URL-encoded, multipart form data, and text content types
- Checks Content-Type header (case-insensitive) and delegates to appropriate handlers
- Implements heuristic content-type detection based on data format when headers are not explicit
- Improves API reliability by automatically choosing correct parsing strategy
- Addresses FIXME comment requesting content-type guessing functionality

Resolves: SmythOS/sre#TODO-content-type-detection
Signed-off-by: versona-tech <[email protected]>
Copy link
Contributor

Choose a reason for hiding this comment

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

The description should be part of the PR, not as a .md file

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing that out.
I’ve moved the description into the PR itself and removed the extra PR_DESCRIPTION.md file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you perform some tests for this implementation ?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I performed local testing and also added automated tests.

  • 7 new tests were added in parseData.test.ts covering JSON, URL-encoded, text, heuristic detection, header priority, backward compatibility, and empty data handling.
  • All tests passed successfully (7/7).
  • Build completed without errors.

Everything works as expected without regressions.

@alaa-eddine-k
Copy link
Contributor

Hi @versona-tech thank you for your contribution, please check the comments on your PR.

@forhad-hosain can you take some time to help in testing these changes and verify that they are not breaking for the UI ?

versona-tech added 2 commits September 20, 2025 22:08
- Add tests for explicit content-type headers (JSON, text)
- Add tests for heuristic detection when no headers present
- Add tests for backward compatibility with existing functionality
- Mock dependencies to isolate parseData behavior
- All 7 test cases passing successfully
- Validates the content-type detection improvements in handleNone function

Relates to SmythOS#154

Signed-off-by: versona-tech <[email protected]>
@versona-tech
Copy link
Author

Hi @versona-tech thank you for your contribution, please check the comments on your PR.

@forhad-hosain can you take some time to help in testing these changes and verify that they are not breaking for the UI ?

Thanks for the feedback!
I’ve addressed the comments and updated the PR.
Looking forward to @forhad-hosain’s feedback on the UI testing.

async function handleNone(body: any, input: any, config, agent: Agent) {
//FIXME: try to guess the content type from headers content-type and data
// Try to guess the content type from headers content-type and data
const configHeaders = config?.headers || {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @versona-tech
Thank you so much for your awesome contribution! 🎉 Your implementation looks great. There’s just a very small adjustment required to make it fully compatible with our UI.

Here’s an example of the config structure we receive from the UI:

config = {
    "id": "<COMPONENT_ID>",
    "name": "APICall",
    "outputs": [...],
    "inputs": [...],
    "data": {
        "method": "POST",
        "url": "https://httpbin.org/post",
        "headers": "{\n  \"Content-Type\": \"multipart/form-data\"\n}",
        "contentType": "none",
        "body": "{\n  \"file\": \"{{file}}\"\n}",
        ...
    },
    ...
}

As you can see, both headers and body are always expected to be strings. So, could you please make sure to handle them accordingly?

This means:
You will receive config.data.headers as a string.
You need to parse it before using it in your implementation.

Once updated, please also adjust the test cases to reflect this behavior.

Thanks again for your great work! 🙌

return await handleMultipartFormData(body, input, config, agent);
} else if (contentTypeHeader.includes('text/')) {
return handleText(body, input, config, agent);
}
Copy link
Contributor

@forhad-hosain forhad-hosain Sep 22, 2025

Choose a reason for hiding this comment

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

@versona-tech Handling binary data is a bit tricky. By setting contentType to binary, we can simply call handleBinary().

But with contentType none, users might specify types like image/png, video/mp4, etc, in the headers.
We could check for common file types and call handleBinary() if there’s a match. What do you think?

@alaa-eddine-k
Copy link
Contributor

@forhad-hosain this might be helpful, I recently migrated many unit-tests from our old repo.
and we have a comprehensive one for APICall component that might be helpful here ==> https://github.com/SmythOS/sre/blob/main/packages/core/tests/unit/008-Components/APICall.test.ts

@versona-tech versona-tech marked this pull request as draft October 3, 2025 01:41
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.

3 participants