Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests for parsing of additional type annotations & refactor test code #18751

Merged
merged 6 commits into from
May 26, 2024
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 @@ -960,7 +960,7 @@ public static boolean shouldGenerateArrayModel(Schema schema) {
* @param schema potentially containing a '$ref'
* @return schema without '$ref'
*/
public static Schema getReferencedSchema(OpenAPI openAPI, Schema schema) {
public static Schema<?> getReferencedSchema(OpenAPI openAPI, Schema schema) {
if (schema == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,39 @@

package org.openapitools.codegen.java;

import org.mockito.Answers;
import org.mockito.Mockito;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.languages.AbstractJavaCodegen;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Collections;

public class AbstractJavaCodegenExampleValuesTest {

private final AbstractJavaCodegen fakeJavaCodegen = new P_AbstractJavaCodegen();
private AbstractJavaCodegen codegen;

/**
* In TEST-NG, test class (and its fields) is only constructed once (vs. for every test in Jupiter),
* using @BeforeMethod to have a fresh codegen mock for each test
*/
@BeforeMethod void mockAbstractCodegen() {
codegen = Mockito.mock(
AbstractJavaCodegen.class, Mockito.withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS).useConstructor()
);
}

@Test
void referencedEnumTakeFirstName() {
final CodegenParameter p = new CodegenParameter();
p.allowableValues = Collections.singletonMap("values", Arrays.asList("first", "second"));
p.dataType = "WrappedEnum";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "WrappedEnum.fromValue(\"first\")");
}

Expand All @@ -48,7 +60,7 @@ void inlineEnum() {
p.isEnum = true;
p.dataType = "String";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "\"first\"");
}

Expand All @@ -61,7 +73,7 @@ void inlineEnumArray() {
p.dataType = "List<String>";
p.items = new CodegenProperty();

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "Arrays.asList()");
}

Expand All @@ -71,7 +83,7 @@ void dateDefault() {
p.isDate = true;
p.dataType = "LocalDate";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "LocalDate.now()");
}

Expand All @@ -82,7 +94,7 @@ void dateGivenExample() {
p.dataType = "LocalDate";
p.example = "2017-03-30";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "LocalDate.parse(\"2017-03-30\")");
}

Expand All @@ -92,7 +104,7 @@ void dateTimeDefault() {
p.isDateTime = true;
p.dataType = "OffsetDateTime";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "OffsetDateTime.now()");
}

Expand All @@ -103,7 +115,7 @@ void dateTimeGivenExample() {
p.dataType = "OffsetDateTime";
p.example = "2007-12-03T10:15:30+01:00";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "OffsetDateTime.parse(\"2007-12-03T10:15:30+01:00\")");
}

Expand All @@ -113,7 +125,7 @@ void uuidDefault() {
p.isUuid = true;
p.dataType = "UUID";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "UUID.randomUUID()");
}

Expand All @@ -124,46 +136,7 @@ void uuidGivenExample() {
p.dataType = "UUID";
p.example = "13b48713-b931-45ea-bd60-b07491245960";

fakeJavaCodegen.setParameterExampleValue(p);
codegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "UUID.fromString(\"13b48713-b931-45ea-bd60-b07491245960\")");
}

private static class P_AbstractJavaCodegen extends AbstractJavaCodegen {
@Override
public CodegenType getTag() {
return null;
}

@Override
public String getName() {
return null;
}

@Override
public String getHelp() {
return null;
}

/**
* Gets artifact version.
* Only for testing purposes.
*
* @return version
*/
public String getArtifactVersion() {
return this.artifactVersion;
}
@Test
void customExampleForEnumValue() {
final AbstractJavaCodegen fakeJavaCodegen = new P_AbstractJavaCodegen();
final CodegenParameter p = new CodegenParameter();
p.allowableValues = Collections.singletonMap("values", Arrays.asList("first", "second"));
p.dataType = "WrappedEnum";
p.example = "CustomEnumValue";

fakeJavaCodegen.setParameterExampleValue(p);
// Custom example value should not be modified
Assert.assertEquals(p.example, "CustomEnumValue");
}
}
}
Loading
Loading