All files / src/plugins/validation/2and3/semantic-validators responses.js

100% Statements 35/35
100% Branches 38/38
100% Functions 6/6
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 10929x 29x 29x 29x     29x     50x               29x 108x   108x   108x 5286x   5286x 248x 537x 300x 98x   98x         84x       84x 7x 21x 7x 7x       14x 14x 14x       3x                                   77x       17x                     237x   236x 236x       7x                       108x    
const each = require('lodash/each');
const { walk, isResponseObject } = require('../../../utils');
const MessageCarrier = require('../../../utils/messageCarrier');
const isPrimitiveType = require('../../../utils/isPrimitiveType');
 
const INLINE_SCHEMA_MESSAGE =
  'Response schemas should be defined with a named ref.';
 
function arrayItemsAreRefOrPrimitive(schema) {
  return (
    schema &&
    schema.type === 'array' &&
    schema.items &&
    (schema.items.$ref || isPrimitiveType(schema.items))
  );
}
 
module.exports.validate = function({ jsSpec, isOAS3 }, config) {
  const messages = new MessageCarrier();
 
  config = config.responses;
 
  walk(jsSpec, [], function(obj, path) {
    const isRef = !!obj.$ref;
 
    if (isResponseObject(path, isOAS3) && !isRef) {
      each(obj, (response, responseKey) => {
        if (isOAS3) {
          each(response.content, (mediaType, mediaTypeKey) => {
            const combinedSchemaTypes = ['allOf', 'oneOf', 'anyOf'];
 
            if (
              mediaType.schema &&
              mediaTypeKey.startsWith('application/json')
            ) {
              const hasCombinedSchema =
                mediaType.schema.allOf ||
                mediaType.schema.anyOf ||
                mediaType.schema.oneOf;
 
              if (hasCombinedSchema) {
                combinedSchemaTypes.forEach(schemaType => {
                  if (mediaType.schema[schemaType]) {
                    for (
                      let i = 0;
                      i < mediaType.schema[schemaType].length;
                      i++
                    ) {
                      const currentSchema = mediaType.schema[schemaType][i];
                      const hasInlineSchema = !currentSchema.$ref;
                      if (
                        hasInlineSchema &&
                        !arrayItemsAreRefOrPrimitive(currentSchema)
                      ) {
                        messages.addMessage(
                          [
                            ...path,
                            responseKey,
                            'content',
                            mediaTypeKey,
                            'schema',
                            schemaType,
                            i
                          ],
                          INLINE_SCHEMA_MESSAGE,
                          config.inline_response_schema,
                          'inline_response_schema'
                        );
                      }
                    }
                  }
                });
              } else if (
                !mediaType.schema.$ref &&
                !arrayItemsAreRefOrPrimitive(mediaType.schema)
              ) {
                messages.addMessage(
                  [...path, responseKey, 'content', mediaTypeKey, 'schema'],
                  INLINE_SCHEMA_MESSAGE,
                  config.inline_response_schema,
                  'inline_response_schema'
                );
              }
            }
          });
        } else {
          // oas 2 allows extensions for responses, dont validate inside of these
          if (responseKey.startsWith('x-')) return;
 
          const hasInlineSchema = response.schema && !response.schema.$ref;
          if (
            hasInlineSchema &&
            !arrayItemsAreRefOrPrimitive(response.schema)
          ) {
            messages.addMessage(
              [...path, responseKey, 'schema'],
              INLINE_SCHEMA_MESSAGE,
              config.inline_response_schema,
              'inline_response_schema'
            );
          }
        }
      });
    }
  });
 
  return messages;
};