Skip to content

Conversation

@vmonnier
Copy link
Contributor

No description provided.

@vmonnier vmonnier requested a review from a team as a code owner August 22, 2025 10:23
@vmonnier vmonnier changed the title Update me_connect.php to enhance the device response json contant check Update me_connect.php to enhance the device response json content check Aug 22, 2025
@vmonnier
Copy link
Contributor Author

Test based on the request header wasn't bullet proof enough :

  1. the "Content-Type:" string search isn't enough flexible : what if there is or not a blank between "Content-Type:" and the "application/json" ?
  2. it's not because we set the header on the request that the cisco will answer a json content. and in fact it appears that the answer is in XML.

From my point of view it's better to simply check if the API call result is a valid Json or not.

@abr-ubiqube abr-ubiqube requested a review from Copilot September 24, 2025 08:49
@abr-ubiqube
Copy link
Member

@vmonnier / @yguubiqube
is this PR still valid ?

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the JSON content detection logic in the Cisco NX REST adapter's connection handling. Instead of relying on Content-Type header checks, it now uses actual JSON parsing to determine if the response contains valid JSON content.

  • Replaces Content-Type header inspection with direct JSON validation using json_decode() and json_last_error()
  • Consolidates JSON processing logic by removing the duplicate content-type checks
  • Maintains existing functionality for session ID extraction and XML conversion

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 169 to 172
json_decode(preg_replace('/":([0-9]+)\.([0-9]+)/', '":"$1.$2"', $result));
if (json_last_error() === JSON_ERROR_NONE ) {
$result=preg_replace('/":([0-9]+)\.([0-9]+)/', '":"$1.$2"', $result);
$array = json_decode ( $result, true );
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

The json_decode() call on line 169 is performing unnecessary work since its return value is discarded. The same regex replacement and JSON decoding is then repeated on line 171. Consider storing the result of the first json_decode() call to avoid duplicate processing.

Suggested change
json_decode(preg_replace('/":([0-9]+)\.([0-9]+)/', '":"$1.$2"', $result));
if (json_last_error() === JSON_ERROR_NONE ) {
$result=preg_replace('/":([0-9]+)\.([0-9]+)/', '":"$1.$2"', $result);
$array = json_decode ( $result, true );
$json_result = preg_replace('/":([0-9]+)\.([0-9]+)/', '":"$1.$2"', $result);
$array = json_decode($json_result, true);
if (json_last_error() === JSON_ERROR_NONE ) {

Copilot uses AI. Check for mistakes.
Copy link
Member

@abr-ubiqube abr-ubiqube left a comment

Choose a reason for hiding this comment

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

some suggestion form copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

4 participants