Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2303,8 +2303,9 @@ public CodegenProperty fromProperty(String name, Schema p) {
// property.baseType = getSimpleRef(p.get$ref());
//}
// --END of revision
property.isModel = ModelUtils.isModel(p);
setNonArrayMapProperty(property, type);
Schema refOrCurrent = ModelUtils.getReferencedSchema(this.openAPI, p);
property.isModel = (ModelUtils.isComposedSchema(refOrCurrent) || ModelUtils.isObjectSchema(refOrCurrent)) && ModelUtils.isModel(refOrCurrent);
}

LOGGER.debug("debugging from property return: " + property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.QueryParameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
Expand Down Expand Up @@ -911,6 +912,41 @@ public void modelWithSuffixDoNotContainInheritedVars() {
Assert.assertEquals(codegenModel.vars.size(), 1);
}

@Test
public void arrayInnerReferencedSchemaMarkedAsModel_20() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/arrayRefBody.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

Set<String> imports = new HashSet<>();

RequestBody body = openAPI.getPaths().get("/examples").getPost().getRequestBody();

CodegenParameter codegenParameter = codegen.fromRequestBody(body, imports, "");

Assert.assertTrue(codegenParameter.isContainer);
Assert.assertTrue(codegenParameter.items.isModel);
Assert.assertFalse(codegenParameter.items.isContainer);
}

@Test
public void arrayInnerReferencedSchemaMarkedAsModel_30() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/arrayRefBody.yaml");
new InlineModelResolver().flatten(openAPI);
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

Set<String> imports = new HashSet<>();

RequestBody body = openAPI.getPaths().get("/examples").getPost().getRequestBody();

CodegenParameter codegenParameter = codegen.fromRequestBody(body, imports, "");

Assert.assertTrue(codegenParameter.isContainer);
Assert.assertTrue(codegenParameter.items.isModel);
Assert.assertFalse(codegenParameter.items.isContainer);
}

@Test
@SuppressWarnings("unchecked")
public void commonLambdasRegistrationTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void generateFormatForDateAndDateTimeQueryParam() throws IOException {
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_2053.yaml", null, new ParseOptions()).getOpenAPI();
.readLocation("src/test/resources/3_0/issue_2053.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
Expand All @@ -188,17 +188,97 @@ public void generateFormatForDateAndDateTimeQueryParam() throws IOException {
generator.opts(input).generate();

checkFileContains(
generator,
outputPath + "/src/main/java/org/openapitools/api/ElephantsApi.java",
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)"
generator,
outputPath + "/src/main/java/org/openapitools/api/ElephantsApi.java",
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)"
);
checkFileContains(
generator,
outputPath + "/src/main/java/org/openapitools/api/ZebrasApi.java",
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)"
generator,
outputPath + "/src/main/java/org/openapitools/api/ZebrasApi.java",
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)"
);
}

@Test
public void shouldGenerateRequestParamForRefParams_3248_Regression() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/3248-regression.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");

ClientOptInput input = new ClientOptInput();
input.setOpenAPI(openAPI);
input.setConfig(codegen);

MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();

checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ExampleApi.java",
"@RequestParam(value = \"format\"",
"@RequestParam(value = \"query\"");
}

@Test
public void shouldGenerateRequestParamForRefParams_3248_RegressionDates() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/3248-regression-dates.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");

ClientOptInput input = new ClientOptInput();
input.setOpenAPI(openAPI);
input.setConfig(codegen);

MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();

checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ExampleApi.java",
"@RequestParam(value = \"start\"");
}

@Test
public void doGenerateRequestParamForSimpleParam() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_3248.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();

checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/MonkeysApi.java", "@RequestParam");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ElephantsApi.java", "@RequestParam");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ZebrasApi.java", "@RequestParam");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/BearsApi.java", "@RequestParam");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/CamelsApi.java", "@RequestParam");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/PandasApi.java", "@RequestParam");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/CrocodilesApi.java", "@RequestParam");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/PolarBearsApi.java", "@RequestParam");

}

private void checkFileNotContains(MockDefaultGenerator generator, String path, String... lines) {
String file = generator.getFiles().get(path);
assertNotNull(file);
Expand All @@ -209,8 +289,14 @@ private void checkFileNotContains(MockDefaultGenerator generator, String path, S
private void checkFileContains(MockDefaultGenerator generator, String path, String... lines) {
String file = generator.getFiles().get(path);
assertNotNull(file);
for (String line : lines)
assertTrue(file.contains(line));
int expectedCount = lines.length;
int actualCount = 0;
for (String line : lines) {
if (file.contains(line)) {
actualCount++;
}
}
assertEquals(actualCount, expectedCount, "File is missing " + (expectedCount - actualCount) + " expected lines.");
}

@Test
Expand Down
52 changes: 52 additions & 0 deletions modules/openapi-generator/src/test/resources/2_0/arrayRefBody.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
swagger: '2.0'
info:
version: ''
title: arrayRefBody
contact: {}
host: www.example.com
basePath: /
schemes:
- https
consumes:
- application/json
produces:
- application/json
paths:
/examples:
post:
description: Create an array of inputs
summary: ''
tags:
- Examples
operationId: createExamples
produces:
- application/json
parameters:
- name: body
in: body
required: true
description: inputs description
schema:
type: array
items:
$ref: '#/definitions/Input'
responses:
200:
description: successful operation
headers: {}
definitions:
Input:
title: Input
type: object
properties:
id:
type: string
age:
type: integer
format: int32
dt:
type: string
format: date-time
tags:
- name: Examples
description: 'Example inputs'
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
openapi: 3.0.2
info:
title: info
description: info
version: 0.1.0

paths:
/example/api:
get:
summary: summary
description: description
parameters:
- name: start
in: query
schema:
type: string
format: date-time
required: true
description: The start time.
responses:
200:
description: response
content:
application/json:
schema:
type: string

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
openapi: 3.0.2
info:
title: info
description: info
version: 0.1.0

paths:
/example/api:
get:
summary: summary
description: description
parameters:
- $ref: '#/components/parameters/requiredQueryParam'
- $ref: '#/components/parameters/formatParam'
responses:
200:
description: response
content:
application/json:
schema:
type: string

components:
parameters:
requiredQueryParam:
description: set query
in: query
name: query
required: true
schema:
type: string
formatParam:
description: set format
in: query
name: format
required: false
schema:
$ref: '#/components/schemas/format'

schemas:
format:
default: json
description: response format
enum:
- json
- csv
type: string
35 changes: 35 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/arrayRefBody.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.0.0
info:
title: ''
version: ''
paths:
/examples:
post:
tags:
- Examples
summary: Get a list of transactions
operationId: getFilteredTransactions
responses:
default:
description: successful operation
requestBody:
description: subscription payload
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Input'
components:
schemas:
Input:
type: object
properties:
id:
type: string
age:
type: integer
format: int32
dt:
type: string
format: date-time
Loading