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 @@ -199,7 +199,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}

{{! begin feature: fluent setter methods for inherited properties }}
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
super.{{setter}}({{name}});
super.{{name}}({{name}});
return this;
}
{{#isArray}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ public JavaFileAssert isAbstractClass() {
return this;
}

public JavaFileAssert assertNoMethod(final String methodName, final String... paramTypes) {
List<MethodDeclaration> methods = paramTypes.length == 0
? actual.getType(0).getMethodsByName(methodName)
: actual.getType(0).getMethodsBySignature(methodName, paramTypes);
String message = paramTypes.length == 0
? "Expected not to find a single method %s, but found " + methods.size()
: "Expected not to find a method %s with parameter(s) %s, but found " + methods.size();
Assertions.assertThat(methods)
.withFailMessage(message, methodName, Arrays.toString(paramTypes))
.isEmpty();
return this;
}

public MethodAssert assertMethod(final String methodName, final String... paramTypes) {
List<MethodDeclaration> methods = paramTypes.length == 0
? actual.getType(0).getMethodsByName(methodName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,63 @@ public void testHasOperationExtraAnnotation_issue12219_array() throws IOExceptio
.containsWithName("org.springframework.security.access.prepost.PreAuthorize");
}

@Test
public void doCallFluentParentSettersFromChildModel() 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_16496.yaml", null, new ParseOptions()).getOpenAPI();

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

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

DefaultGenerator generator = new DefaultGenerator();

generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
generator.opts(input).generate();


JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Animal.java"))
// Fluent method assertions
.assertMethod("alias")
.hasReturnType("Animal")
.bodyContainsLines("this.alias = JsonNullable.of(alias);", "return this;")
.hasParameter("alias")
.withType("String")
.toMethod()
.toFileAssert()

// Setter method assertions
.assertMethod("setAlias")
.hasReturnType("void")
.hasParameter("alias")
.withType("JsonNullable<String>");

JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Zebra.java"))
// Fluent method assertions
.assertMethod("alias")
.hasReturnType("Zebra")
.bodyContainsLines("super.alias(alias);", "return this;")
.hasParameter("alias")
.withType("String")
.toMethod()
.toFileAssert()

// No overridden setter on child object
.assertNoMethod("setAlias");
}

@Test
public void multiLineOperationDescription() throws IOException {
Expand Down
56 changes: 56 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_16496.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
openapi: 3.0.0
servers:
- url: 'localhost:8080'
info:
version: 1.0.0
title: OpenAPI Zoo
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
/zebras:
get:
operationId: getZebras
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Zebra'

components:
schemas:
Animal:
type: object
discriminator:
propertyName: type
properties:
type:
type: string
enum:
- zebra
name:
type: string
example: 'Marty'
age:
type: integer
example: 15
alias:
type: string
nullable: true
description: 'The stripy one'
example: 'Stripy'

Zebra:
allOf:
- $ref: '#/components/schemas/Animal'
- type: object
properties:
stripePattern:
type: string
enum:
- horizontal
- vertical
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ public void setKind(KindEnum kind) {


public BigCat declawed(Boolean declawed) {
super.setDeclawed(declawed);
super.declawed(declawed);
return this;
}

public BigCat className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public BigCat color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public void setDeclawed(Boolean declawed) {


public Cat className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public Cat color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public void setBreed(String breed) {


public Dog className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public Dog color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ public void setKind(KindEnum kind) {


public BigCatDto declawed(Boolean declawed) {
super.setDeclawed(declawed);
super.declawed(declawed);
return this;
}

public BigCatDto className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public BigCatDto color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public void setDeclawed(Boolean declawed) {


public CatDto className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public CatDto color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public void setBreed(String breed) {


public DogDto className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public DogDto color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ public void setKind(KindEnum kind) {


public BigCat declawed(Boolean declawed) {
super.setDeclawed(declawed);
super.declawed(declawed);
return this;
}

public BigCat className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public BigCat color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public void setDeclawed(Boolean declawed) {


public Cat className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public Cat color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public void setBreed(String breed) {


public Dog className(String className) {
super.setClassName(className);
super.className(className);
return this;
}

public Dog color(String color) {
super.setColor(color);
super.color(color);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,22 @@ public void setFoo(FooRefOrValue foo) {


public Bar href(String href) {
super.setHref(href);
super.href(href);
return this;
}

public Bar atSchemaLocation(String atSchemaLocation) {
super.setAtSchemaLocation(atSchemaLocation);
super.atSchemaLocation(atSchemaLocation);
return this;
}

public Bar atBaseType(String atBaseType) {
super.setAtBaseType(atBaseType);
super.atBaseType(atBaseType);
return this;
}

public Bar atType(String atType) {
super.setAtType(atType);
super.atType(atType);
return this;
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,27 @@ public void setFoo(FooRefOrValue foo) {


public BarCreate href(String href) {
super.setHref(href);
super.href(href);
return this;
}

public BarCreate id(String id) {
super.setId(id);
super.id(id);
return this;
}

public BarCreate atSchemaLocation(String atSchemaLocation) {
super.setAtSchemaLocation(atSchemaLocation);
super.atSchemaLocation(atSchemaLocation);
return this;
}

public BarCreate atBaseType(String atBaseType) {
super.setAtBaseType(atBaseType);
super.atBaseType(atBaseType);
return this;
}

public BarCreate atType(String atType) {
super.setAtType(atType);
super.atType(atType);
return this;
}
@Override
Expand Down
Loading