Skip to content

Commit 1c3984a

Browse files
authored
Merge branch 'master' into typescript-date-format
2 parents 517ac00 + 5fdfaa4 commit 1c3984a

File tree

5 files changed

+302
-4
lines changed

5 files changed

+302
-4
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,9 @@ public String getSwaggerType(Property p) {
11591159
try {
11601160
RefProperty r = (RefProperty) p;
11611161
datatype = r.get$ref();
1162-
if (datatype.indexOf("#/definitions/") == 0) {
1163-
datatype = datatype.substring("#/definitions/".length());
1162+
// '#/definitions' or ../../../../relative-ref/nested/directory/definitions/photos.yml#/definitions/
1163+
if (datatype.indexOf("#/definitions/") >= 0) {
1164+
datatype = datatype.substring(datatype.indexOf("#/definitions/") + "#/definitions/".length());
11641165
}
11651166
} catch (Exception e) {
11661167
LOGGER.warn("Error obtaining the datatype from RefProperty:" + p + ". Datatype default to Object");
@@ -2006,7 +2007,7 @@ protected void setNonArrayMapProperty(CodegenProperty property, String type) {
20062007
/**
20072008
* Override with any special handling of response codes
20082009
* @param responses Swagger Operation's responses
2009-
* @return default method response or <tt>null</tt> if not found
2010+
* @return default method response or &lt;tt&gt;null&lt;/tt&gt; if not found
20102011
*/
20112012
protected Response findMethodResponse(Map<String, Response> responses) {
20122013

modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,51 @@ public void multiInheritanceOfComposedModelTest() {
366366
Assert.assertEquals(child.parent, "ChildOfComposedParent");
367367
}
368368

369+
@Test(description = "use relative $ref for definitions of parameters")
370+
public void relativeDefinitionsInParameterTest() {
371+
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/relative-ref/nested/directory/main/relative-refs.yml");
372+
final DefaultCodegen codegen = new DefaultCodegen();
373+
final String path = "/photo/getPhotos";
374+
final Operation p = model.getPaths().get(path).getPost();
375+
CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions(), model);
376+
377+
Assert.assertNotNull(op);
378+
Assert.assertNotNull(op.imports);
379+
Assert.assertTrue(op.imports.contains("Photo"));
380+
Assert.assertTrue(op.imports.contains("PhotosRequest"));
381+
382+
}
369383

384+
@Test(description = "use relative $ref for definitions of response")
385+
public void relativeDefinitionsInResponseTest() {
386+
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/relative-ref/nested/directory/main/relative-refs.yml");
387+
final DefaultCodegen codegen = new DefaultCodegen();
388+
final String path = "/photo/{id}";
389+
final Operation p = model.getPaths().get(path).getGet();
390+
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
391+
392+
Assert.assertNotNull(op);
393+
Assert.assertNotNull(op.imports);
394+
Assert.assertTrue(op.imports.contains("Photo"));
395+
Assert.assertTrue(op.imports.contains("integer"));
396+
397+
}
398+
399+
@Test(description = "use relative $ref for definitions of response")
400+
public void relativeDefinitionsMapInResponseTest() {
401+
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/relative-ref/nested/directory/main/relative-refs.yml");
402+
final DefaultCodegen codegen = new DefaultCodegen();
403+
final String path = "/photo/thumbnails";
404+
final Operation p = model.getPaths().get(path).getPost();
405+
CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions(), model);
406+
407+
Assert.assertNotNull(op);
408+
Assert.assertNotNull(op.imports);
409+
Assert.assertTrue(op.imports.contains("Photo"));
410+
Assert.assertTrue(op.imports.contains("PhotoThumbnailsRequest"));
411+
412+
}
413+
370414
@Test(description = "use operation consumes and produces")
371415
public void localConsumesAndProducesTest() {
372416
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/globalConsumesAndProduces.json");

modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.codegen;
22

3+
import io.swagger.codegen.config.CodegenConfigurator;
34
import io.swagger.codegen.languages.JavaClientCodegen;
45
import io.swagger.models.ExternalDocs;
56
import io.swagger.models.Swagger;
@@ -225,7 +226,54 @@ public void testIssue9132() throws Exception {
225226

226227
}
227228

228-
private boolean containsOverloadedComments(File file, String ...search) throws IOException {
229+
@Test
230+
public void testRelativeRefs() throws IOException {
231+
final File output = folder.getRoot();
232+
233+
CodegenConfigurator codegenConfigurator = new CodegenConfigurator();
234+
codegenConfigurator.setInputSpec("src/test/resources/2_0/relative-ref/nested/directory/main/relative-refs.yml");
235+
codegenConfigurator.setOutputDir(output.getAbsolutePath());
236+
codegenConfigurator.setLang("java");
237+
238+
Map<String, Object> additionalProperties = new HashMap<>();
239+
additionalProperties.put("dateLibrary", "java8");
240+
additionalProperties.put("library", "feign");
241+
additionalProperties.put("apiTests", false);
242+
additionalProperties.put("hideGenerationTimestamp", true);
243+
additionalProperties.put("invokerPackage", "com.mycompany.generated.client");
244+
additionalProperties.put("modelPackage", "com.mycompany.generated.client.model");
245+
additionalProperties.put("apiPackage", "com.mycompany.generated.client.api");
246+
247+
codegenConfigurator.setAdditionalProperties(additionalProperties);
248+
249+
Map<String, String> importMapping = new HashMap<>();
250+
251+
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
252+
importMapping.put("LocalTime", "java.time.LocalTime");
253+
importMapping.put("DayOfWeek", "java.time.DayOfWeek");
254+
importMapping.put("Duration", "java.time.Duration");
255+
importMapping.put("ChronoUnit", "java.time.temporal.ChronoUnit");
256+
importMapping.put("Currency", "java.util.Currency");
257+
importMapping.put("LocalDate", "java.time.LocalDate");
258+
importMapping.put("Locale", "java.util.Locale");
259+
importMapping.put("ZoneId", "java.time.ZoneId");
260+
261+
codegenConfigurator.setImportMappings(importMapping);
262+
263+
DefaultGenerator generator = new DefaultGenerator();
264+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
265+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
266+
generator.setGeneratorPropertyDefault(CodegenConstants.API_TESTS, "false");
267+
generator.setGeneratorPropertyDefault(CodegenConstants.API_DOCS, "false");
268+
269+
//generate
270+
generator.opts(codegenConfigurator.toClientOptInput()).generate();
271+
final File defaultApi = new File(output, "src/main/java/com/mycompany/generated/client/api/DefaultApi.java");
272+
assertTrue(defaultApi.exists());
273+
assertFalse(containsSearchStrings(defaultApi,"RelativeRefnesteddirectorydefinitionsphotosYmldefinitionsPhoto"));
274+
}
275+
276+
private boolean containsSearchStrings(File file, String ...search) throws IOException {
229277
for (String line : Files.readAllLines(file.toPath(), Charset.defaultCharset())) {
230278
if (StringUtils.containsAny(line, search)) {
231279
return true;
@@ -235,6 +283,10 @@ private boolean containsOverloadedComments(File file, String ...search) throws I
235283
return false;
236284
}
237285

286+
private boolean containsOverloadedComments(File file, String ...search) throws IOException {
287+
return containsSearchStrings(file, search);
288+
}
289+
238290
@Test
239291
public void testOverloadingTemplateFiles() throws Exception {
240292
final File output = folder.getRoot();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
swagger: '2.0'
2+
info:
3+
title: Photos API
4+
description: Photo service
5+
6+
schemes:
7+
- http
8+
9+
produces:
10+
- application/json
11+
12+
# prefix for all paths
13+
# for breaking changes, we need to change the basePath and add a second swagger file
14+
basePath: /v1
15+
16+
paths:
17+
/photo/photos:
18+
post:
19+
operationId: getPhotos
20+
description: Retrieve photos by ids
21+
parameters:
22+
- name: photosRequest
23+
in: body
24+
required: true
25+
description: The photos being requested
26+
schema:
27+
$ref: '#/definitions/PhotosRequest'
28+
responses:
29+
200:
30+
description: A collection of Photos
31+
schema:
32+
type: array
33+
items:
34+
$ref: '#/definitions/Photo'
35+
36+
definitions:
37+
Photo:
38+
properties:
39+
id:
40+
type: integer
41+
format: int32
42+
description: The photo id (always present in the response)
43+
caption:
44+
type: string
45+
description: |
46+
Caption to be shown for the photo in the UI.
47+
uploadDate:
48+
type: string
49+
format: date
50+
description: The upload date for the image (requested using PhotoField.UPLOAD_DATE)
51+
uploadDateTime:
52+
type: string
53+
format: date-time
54+
description: The upload date-time (in UTC) for the image (requested using PhotoField.UPLOAD_DATETIME)
55+
56+
PhotosRequest:
57+
required:
58+
- photoIds
59+
- photoFields
60+
properties:
61+
photoIds:
62+
type: array
63+
items:
64+
type: integer
65+
format: int32
66+
photoFields:
67+
description: |
68+
The fields of a photo object to be retrieved.
69+
If none are specified, only the id comes back
70+
type: array
71+
items:
72+
$ref: '#/definitions/PhotoField'
73+
74+
PhotoField:
75+
type: string
76+
enum:
77+
- CAPTION
78+
- UPLOAD_DATE
79+
- UPLOAD_DATETIME
80+
81+
PhotoThumbnailsRequest:
82+
required:
83+
- ids
84+
- photoFields
85+
properties:
86+
ids:
87+
type: array
88+
items:
89+
type: integer
90+
format: int32
91+
photoFields:
92+
description: |
93+
The fields of a photo object to be retrieved.
94+
If none are specified, only the id comes back
95+
type: array
96+
items:
97+
$ref: '#/definitions/PhotoField'
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
swagger: '2.0'
2+
info:
3+
title: API Definition with Relative models
4+
description: This is an example
5+
schemes:
6+
- https
7+
- http
8+
9+
produces:
10+
- application/json
11+
12+
basePath: /v1
13+
14+
paths:
15+
/photo/getPhotos:
16+
post:
17+
operationId: getPhotos
18+
description: Retrieve photos by ids
19+
parameters:
20+
- name: photosRequest
21+
in: body
22+
required: true
23+
description: The photos being requested
24+
schema:
25+
$ref: '../../../../relative-ref/nested/directory/definitions/photos.yml#/definitions/PhotosRequest'
26+
responses:
27+
200:
28+
description: A collection of Photos
29+
schema:
30+
type: array
31+
items:
32+
$ref: '../../../../relative-ref/nested/directory/definitions/photos.yml#/definitions/Photo'
33+
34+
/photo/{id}:
35+
get:
36+
operationId: get photo by Id
37+
description: Retrieve photo by id
38+
parameters:
39+
- name: "id"
40+
in: "path"
41+
description: "id"
42+
required: true
43+
type: integer
44+
format: int32
45+
responses:
46+
200:
47+
description: A collection of Photos
48+
schema:
49+
type: array
50+
items:
51+
$ref: '../../../../relative-ref/nested/directory/definitions/photos.yml#/definitions/Photo'
52+
53+
/photo/getPhotoPreview/{id}:
54+
post:
55+
operationId: getPhotoPreview
56+
description: get thumbnail preview of a photo
57+
parameters:
58+
- name: id
59+
in: path
60+
type: integer
61+
minimum: 1
62+
required: true
63+
description: the id of photo thumbnail requested
64+
responses:
65+
200:
66+
description: the response containing a preview thumbnail, along with some additional information about the photo
67+
schema:
68+
$ref: '#/definitions/PhotoPreview'
69+
70+
/photo/thumbnails:
71+
post:
72+
operationId: getThumbnails
73+
description: Retrieve photo thumbnails by ids
74+
parameters:
75+
- name: photoThumbnailsRequest
76+
in: body
77+
required: true
78+
description: The photos being requested
79+
schema:
80+
$ref: '../../../../relative-ref/nested/directory/definitions/photos.yml#/definitions/PhotoThumbnailsRequest'
81+
responses:
82+
200:
83+
description: |
84+
A map of id -> photo for requested ids.
85+
schema:
86+
type: object
87+
additionalProperties:
88+
$ref: '../../../../relative-ref/nested/directory/definitions/photos.yml#/definitions/Photo'
89+
90+
definitions:
91+
PhotoPreview:
92+
type: object
93+
properties:
94+
id:
95+
description: the id of the newly inserted photo
96+
type: integer
97+
format: int32
98+
caption:
99+
description: the caption of the photo
100+
type: string
101+
thumbnail:
102+
description: The media binary of thumbnail
103+
type: string
104+
format: byte

0 commit comments

Comments
 (0)