|
| 1 | +/* |
| 2 | + * Copyright 2019 the original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.github.hauner.openapi.spring.converter |
| 18 | + |
| 19 | +import com.github.hauner.openapi.spring.generatr.ApiOptions |
| 20 | +import com.github.hauner.openapi.spring.generatr.mapping.TypeMapping |
| 21 | +import com.github.hauner.openapi.spring.model.Api |
| 22 | +import spock.lang.Specification |
| 23 | + |
| 24 | +import static com.github.hauner.openapi.spring.support.OpenApiParser.parse |
| 25 | + |
| 26 | +class DataTypesConverterGlobalTypeMappingSpec extends Specification { |
| 27 | + |
| 28 | + void "converts named schemas to java type via global type mapping" () { |
| 29 | + def openApi = parse ("""\ |
| 30 | +openapi: 3.0.2 |
| 31 | +info: |
| 32 | + title: API |
| 33 | + version: 1.0.0 |
| 34 | +
|
| 35 | +paths: |
| 36 | + /page: |
| 37 | + get: |
| 38 | + parameters: |
| 39 | + - in: query |
| 40 | + name: pageable |
| 41 | + required: false |
| 42 | + schema: |
| 43 | + \$ref: '#/components/schemas/Pageable' |
| 44 | + responses: |
| 45 | + '200': |
| 46 | + description: none |
| 47 | + content: |
| 48 | + application/json: |
| 49 | + schema: |
| 50 | + \$ref: '#/components/schemas/StringPage' |
| 51 | +
|
| 52 | +components: |
| 53 | + schemas: |
| 54 | +
|
| 55 | + Pageable: |
| 56 | + description: minimal Pageable query parameters |
| 57 | + type: object |
| 58 | + properties: |
| 59 | + page: |
| 60 | + type: integer |
| 61 | + size: |
| 62 | + type: integer |
| 63 | +
|
| 64 | + Page: |
| 65 | + description: minimal Page response without content property |
| 66 | + type: object |
| 67 | + properties: |
| 68 | + number: |
| 69 | + type: integer |
| 70 | + size: |
| 71 | + type: integer |
| 72 | +
|
| 73 | + StringContent: |
| 74 | + description: specific content List of the Page response |
| 75 | + type: object |
| 76 | + properties: |
| 77 | + content: |
| 78 | + type: array |
| 79 | + items: |
| 80 | + type: string |
| 81 | +
|
| 82 | + StringPage: |
| 83 | + description: typed Page |
| 84 | + type: object |
| 85 | + allOf: |
| 86 | + - \$ref: '#/components/schemas/Page' |
| 87 | + - \$ref: '#/components/schemas/StringContent' |
| 88 | +""") |
| 89 | + |
| 90 | + when: |
| 91 | + def options = new ApiOptions( |
| 92 | + packageName: 'pkg', |
| 93 | + typeMappings: [ |
| 94 | + new TypeMapping ( |
| 95 | + sourceTypeName: 'Pageable', |
| 96 | + targetTypeName: 'org.springframework.data.domain.Pageable'), |
| 97 | + new TypeMapping ( |
| 98 | + sourceTypeName: 'StringPage', |
| 99 | + targetTypeName: 'org.springframework.data.domain.Page<String>') |
| 100 | + ]) |
| 101 | + Api api = new ApiConverter (options).convert (openApi) |
| 102 | + |
| 103 | + then: |
| 104 | + def itf = api.interfaces.first () |
| 105 | + def ep = itf.endpoints.first () |
| 106 | + def parameter = ep.parameters.first () |
| 107 | + def response = ep.response |
| 108 | + parameter.dataType.pkg == 'org.springframework.data.domain' |
| 109 | + parameter.dataType.name == 'Pageable' |
| 110 | + response.responseType.pkg == 'org.springframework.data.domain' |
| 111 | + response.responseType.name == 'Page<String>' |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments