Skip to content

Commit

Permalink
Fix test connection with codemirror and extra (#35122)
Browse files Browse the repository at this point in the history
  • Loading branch information
vchiapaikeo authored Oct 23, 2023
1 parent ef497bc commit 789222c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions airflow/www/static/js/connection_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const configTestConnection = getMetaValue("config_test_connection")
const restApiEnabled = getMetaValue("rest_api_enabled") === "True";
const connectionTestUrl = getMetaValue("test_url");

// Define editor var which may get populated if extra field exists on the connection
let editor;

function decode(str) {
return new DOMParser().parseFromString(str, "text/html").documentElement
.textContent;
Expand Down Expand Up @@ -330,6 +333,11 @@ $(document).ready(() => {
$("#test-connection").on("click", (e) => {
e.preventDefault();
hideAlert();
// save the contents of the CodeMirror editor to the textArea if it is populated
// (i.e., connection type has extra field)
if (Object.prototype.hasOwnProperty.call(editor, "save")) {
editor.save();
}
$.ajax({
url: connectionTestUrl,
type: "post",
Expand All @@ -356,16 +364,18 @@ $(document).ready(() => {

// Change conn.extra TextArea widget to CodeMirror
const textArea = document.getElementById("extra");
const editor = CodeMirror.fromTextArea(textArea, {
editor = CodeMirror.fromTextArea(textArea, {
mode: { name: "javascript", json: true },
gutters: ["CodeMirror-lint-markers"],
lineWrapping: true,
lint: true,
});

// beautify JSON
// beautify JSON but only if it is not equal to default value of empty string
const jsonData = editor.getValue();
const data = JSON.parse(jsonData);
const formattedData = JSON.stringify(data, null, 2);
editor.setValue(formattedData);
if (jsonData !== "") {
const data = JSON.parse(jsonData);
const formattedData = JSON.stringify(data, null, 2);
editor.setValue(formattedData);
}
});

0 comments on commit 789222c

Please sign in to comment.