Skip to content

Commit f3a81ac

Browse files
committed
Add "Save as" protections for definitions that are not valid YAML/JSON
1 parent 5601c0c commit f3a81ac

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/standalone/topbar/topbar.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ export default class Topbar extends React.Component {
7979
let language = this.getDefinitionLanguage()
8080
let fileName = this.getFileName()
8181

82+
if(this.hasParserErrors()) {
83+
if(language === "yaml") {
84+
const shouldContinue = confirm("Swagger-Editor isn't able to parse your API definition. Are you sure you want to save the editor content as YAML?")
85+
if(!shouldContinue) return
86+
} else {
87+
return alert("Save as YAML is not currently possible because Swagger-Editor wasn't able to parse your API definiton.")
88+
}
89+
}
90+
8291
if(language === "yaml") {
8392
//// the content is YAML,
8493
//// so download as-is
@@ -99,6 +108,12 @@ export default class Topbar extends React.Component {
99108
let editorContent = this.props.specSelectors.specStr()
100109
let fileName = this.getFileName()
101110

111+
if(this.hasParserErrors()) {
112+
// we can't recover from a parser error in save as JSON
113+
// because we are always parsing so we can beautify
114+
return alert("Save as JSON is not currently possible because Swagger-Editor wasn't able to parse your API definiton.")
115+
}
116+
102117
// JSON or YAML String -> JS object
103118
let jsContent = YAML.safeLoad(editorContent)
104119
// JS Object -> pretty JSON string
@@ -186,6 +201,10 @@ export default class Topbar extends React.Component {
186201

187202
// Logic helpers
188203

204+
hasParserErrors = () => {
205+
return this.props.errSelectors.allErrors().filter(err => err.get("source") === "parser").size > 0
206+
}
207+
189208
getFileName = () => {
190209
// Use `isSwagger2` here, because we want to default to `openapi` if we don't know.
191210
if(this.props.specSelectors.isSwagger2 && this.props.specSelectors.isSwagger2()) {
@@ -286,6 +305,7 @@ export default class Topbar extends React.Component {
286305

287306
Topbar.propTypes = {
288307
specSelectors: PropTypes.object.isRequired,
308+
errSelectors: PropTypes.object.isRequired,
289309
specActions: PropTypes.object.isRequired,
290310
getComponent: PropTypes.func.isRequired
291311
}

0 commit comments

Comments
 (0)