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

Fix test connection with codemirror and extra #35122

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});
Loading