title: Iterating over Swagger YAML File to dynamically generate a list of properties
body: <p>I am trying to create a script that can accept a swagger yml file (like the example petstore.yaml), and generate a list of used "attributes" from the file. </p>
  
  <p>This involves the parsing of yaml, then iterating over all elements in the json object to return the required data. I intend to traverse all the paths to identify valid responses, but for now I just want to  filter out all definitions specified in the yaml file, then for each def, output the list of properties.</p>
  
  <p>a sample yaml file can be downloaded <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/yaml/petstore.yaml" rel="nofollow noreferrer">here</a></p>
  
  <p>at the end of the day, i would like to generate a string for each attribute which shows something like </p>
  
  <pre><code>&lt;filename&gt;~Pet~id~integer~int64
  &lt;filename&gt;~Pet~name~string~
  &lt;filename&gt;~Pet~tag~string~
  </code></pre>
  
  <p>for this, i need to locate the 'definitions' node, and iterate over all sub-nodes to read the info. </p>
  
  <p>I am struggling to get the logic right for a yaml styled file.. Below is my working code. </p>
  
  <p>It just feels to me like I have over-complicated the iterative looping (maybe a better solution would be to use regex? </p>
  
  <p>index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;title&gt;Read YAML&lt;/title&gt;
          &lt;/script&gt;&lt;script src='https://cdnjs.cloudflare.com/ajax/libs/js-yaml/3.13.1/js-yaml.min.js'&gt;            
      &lt;/script&gt;
      &lt;body&gt;
          &lt;input type="file" id="file-input" /&gt;
          &lt;h3&gt;Contents of the file:&lt;/h3&gt;
          &lt;pre id="file-content"&gt;&lt;/pre&gt;
          &lt;div id='output'&gt;&lt;/div&gt;
      &lt;/body&gt;
      &lt;script&gt;
          var yaml = window.jsyaml;
          &lt;/script&gt;
      &lt;script src="app.js"&gt;&lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>my javascript file </p>
  
  <pre><code>var body = '';
  var mystring = '' 
  function readSingleFile(e) {
      var file = e.target.files[0];
      if (!file) {
        return;
      }
      var reader = new FileReader();
      reader.onload = function(e) {
        var contents = e.target.result;
        displayContents(contents);
      };
      reader.readAsText(file);
  }
  
  function displayContents(contents) {
        var element = document.getElementById('file-content');
        var doc = yaml.load(contents);
        // Process the file.
        // Process only the definitions section of the file
        var definition = doc.definitions
        console.log (definition) ;
        for(var def in definition) {
          if(definition.hasOwnProperty(def)) {
            var node = definition[def]
              if (typeof(node) === 'object') {
                  // loop through the definitions
                  processContents(node)
              }
          }
        }
  
        function processContents(obj) {
          for (var item in obj) {
                var definitions = obj[item]
                if (typeof definitions === 'object'){
                  for (var attribute in definitions) {
                    // HELP -- if there is an attribute called "properties"  then iterate over all properties and create the concatenated string
                    // Broken from here !!!!
                    if (attribute.properties) {
                        for (var param  in attribute.properties) {
                          mystring = param.name + '~' + param.type + '~' + param.description + '~' + param.format
                        }
                    }
                }
                }
          }
  
        }
        document.getElementById('output').innerHTML = body;
   }
  
  document.getElementById('file-input')
    .addEventListener('change', readSingleFile, false);
  </code></pre>