mirror of
				https://github.com/Redocly/redoc.git
				synced 2025-11-04 01:37:32 +03:00 
			
		
		
		
	
							parent
							
								
									418b74848d
								
							
						
					
					
						commit
						633d71293f
					
				| 
						 | 
				
			
			@ -17,6 +17,21 @@
 | 
			
		|||
        "schema": { "type": "array" },
 | 
			
		||||
        "style": "form",
 | 
			
		||||
        "explode": true
 | 
			
		||||
      },
 | 
			
		||||
      "queryParamWithNoStyle": {
 | 
			
		||||
        "in": "query",
 | 
			
		||||
        "name": "serialization_test_name",
 | 
			
		||||
        "schema": { "type": "array" }
 | 
			
		||||
      },
 | 
			
		||||
      "pathParamWithNoStyle": {
 | 
			
		||||
        "in": "path",
 | 
			
		||||
        "name": "serialization_test_name",
 | 
			
		||||
        "schema": { "type": "array" }
 | 
			
		||||
      },
 | 
			
		||||
      "cookieParamWithNoStyle": {
 | 
			
		||||
        "in": "cookie",
 | 
			
		||||
        "name": "serialization_test_name",
 | 
			
		||||
        "schema": { "type": "array" }
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "headers": {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,6 +43,57 @@ describe('Models', () => {
 | 
			
		|||
      expect(field.explode).toEqual(true);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('field details relevant for default path param serialization', () => {
 | 
			
		||||
      const field = new FieldModel(
 | 
			
		||||
        parser,
 | 
			
		||||
        {
 | 
			
		||||
          $ref: '#/components/parameters/pathParamWithNoStyle',
 | 
			
		||||
        },
 | 
			
		||||
        '#/components/parameters/pathParamWithNoStyle',
 | 
			
		||||
        opts,
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      expect(field.name).toEqual('serialization_test_name');
 | 
			
		||||
      expect(field.in).toEqual('path');
 | 
			
		||||
      expect(field.schema.type).toEqual('array');
 | 
			
		||||
      expect(field.style).toEqual('simple');
 | 
			
		||||
      expect(field.explode).toEqual(false);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('field details relevant for default query parameter serialization', () => {
 | 
			
		||||
      const field = new FieldModel(
 | 
			
		||||
        parser,
 | 
			
		||||
        {
 | 
			
		||||
          $ref: '#/components/parameters/queryParamWithNoStyle',
 | 
			
		||||
        },
 | 
			
		||||
        '#/components/parameters/queryParamWithNoStyle',
 | 
			
		||||
        opts,
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      expect(field.name).toEqual('serialization_test_name');
 | 
			
		||||
      expect(field.in).toEqual('query');
 | 
			
		||||
      expect(field.schema.type).toEqual('array');
 | 
			
		||||
      expect(field.style).toEqual('form');
 | 
			
		||||
      expect(field.explode).toEqual(true);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('field details relevant for default cookie parameter serialization', () => {
 | 
			
		||||
      const field = new FieldModel(
 | 
			
		||||
        parser,
 | 
			
		||||
        {
 | 
			
		||||
          $ref: '#/components/parameters/queryParamWithNoStyle',
 | 
			
		||||
        },
 | 
			
		||||
        '#/components/parameters/queryParamWithNoStyle',
 | 
			
		||||
        opts,
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      expect(field.name).toEqual('serialization_test_name');
 | 
			
		||||
      expect(field.in).toEqual('query');
 | 
			
		||||
      expect(field.schema.type).toEqual('array');
 | 
			
		||||
      expect(field.style).toEqual('form');
 | 
			
		||||
      expect(field.explode).toEqual(true);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('field name should populated from name even if $ref (headers)', () => {
 | 
			
		||||
      const field = new FieldModel(
 | 
			
		||||
        parser,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,18 +12,27 @@ import { extractExtensions } from '../../utils/openapi';
 | 
			
		|||
import { OpenAPIParser } from '../OpenAPIParser';
 | 
			
		||||
import { SchemaModel } from './Schema';
 | 
			
		||||
 | 
			
		||||
function getDefaultStyleValue(parameterLocation: OpenAPIParameterLocation): OpenAPIParameterStyle {
 | 
			
		||||
  switch (parameterLocation) {
 | 
			
		||||
    case 'header':
 | 
			
		||||
      return 'simple';
 | 
			
		||||
    case 'query':
 | 
			
		||||
      return 'form';
 | 
			
		||||
    case 'path':
 | 
			
		||||
      return 'simple';
 | 
			
		||||
    default:
 | 
			
		||||
      return 'form';
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
const DEFAULT_SERIALIZATION: Record<
 | 
			
		||||
  OpenAPIParameterLocation,
 | 
			
		||||
  { explode: boolean; style: OpenAPIParameterStyle }
 | 
			
		||||
> = {
 | 
			
		||||
  path: {
 | 
			
		||||
    style: 'simple',
 | 
			
		||||
    explode: false,
 | 
			
		||||
  },
 | 
			
		||||
  query: {
 | 
			
		||||
    style: 'form',
 | 
			
		||||
    explode: true,
 | 
			
		||||
  },
 | 
			
		||||
  header: {
 | 
			
		||||
    style: 'simple',
 | 
			
		||||
    explode: false,
 | 
			
		||||
  },
 | 
			
		||||
  cookie: {
 | 
			
		||||
    style: 'form',
 | 
			
		||||
    explode: true,
 | 
			
		||||
  },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Field or Parameter model ready to be used by components
 | 
			
		||||
| 
						 | 
				
			
			@ -75,10 +84,14 @@ export class FieldModel {
 | 
			
		|||
    } else if (info.style) {
 | 
			
		||||
      this.style = info.style;
 | 
			
		||||
    } else if (this.in) {
 | 
			
		||||
      this.style = getDefaultStyleValue(this.in);
 | 
			
		||||
      this.style = DEFAULT_SERIALIZATION[this.in].style;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (info.explode === undefined && this.in) {
 | 
			
		||||
      this.explode = DEFAULT_SERIALIZATION[this.in].explode;
 | 
			
		||||
    } else {
 | 
			
		||||
      this.explode = !!info.explode;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.deprecated = info.deprecated === undefined ? !!this.schema.deprecated : info.deprecated;
 | 
			
		||||
    parser.exitRef(infoOrRef);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user