Skip to content

Commit

Permalink
Handle short/long integer enums (#770)
Browse files Browse the repository at this point in the history
This fixes #771
  • Loading branch information
jskov-jyskebank-dk authored Jan 3, 2025
1 parent b611629 commit 3ac61fd
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ private List<String> addUnknownDefault(Type enumType, List<String> values) {
private CtxEnumEntry toEnumEntry(Type enumType, EnumNameValue e) {
String name = e.name();
String value = e.value();
if (enumType == Primitive.INT) {
if (enumType == Primitive.SHORT || enumType == Primitive.NOFORMAT_INT) {
value = "(short)" + value;
} else if (enumType == Primitive.INT) {
if (opts.isUseEnumUnknownDefault() && ENUM_INT_UNKNOWN_DEFAULT_STR.equals(value)) {
name = ENUM_UNKNOWN_DEFAULT_OPEN_API.toUpperCase(Locale.ROOT);
}
} else if (enumType == Primitive.LONG) {
value = value + "L";
} else {
value = StringRenderer.quote(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ List<DynamicTest> makeTests() throws IOException {
// Replace with partial test name (or empty to run all tests)
// Handy when working on a single test
// String testNameContains = "manual";
String testNameContains = "responses_ref";
String testNameContains = "enums/integers";

boolean runAllTests = Boolean.parseBoolean(System.getProperty("run_all_tests"));
Predicate<? super Path> filterByProperty = p -> testDir.isEmpty() || p.toString().contains(testDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* openapi API Title
* openapi API description
*
* The version of the OpenAPI document: openapi API Version
*/

package mada.tests.e2e.dto.enums.integers.api;

import javax.ws.rs.*;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;

@javax.annotation.processing.Generated(value = "dk.mada.jaxrs.Generator")
@Path("/v1/customers/engagements")
public interface Customer_InformationApi {

/**
* Get engagements (refNo) for a specific customer.
*/
@GET
@APIResponses({
@APIResponse(responseCode = "200", description = "")
})
@Operation(summary = "Get engagements (refNo) for a specific customer.")
void getCustomerEngagement();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* openapi API Title
* openapi API description
*
* The version of the OpenAPI document: openapi API Version
*/

package mada.tests.e2e.dto.enums.integers.dto;

import java.util.Objects;
import javax.json.bind.annotation.JsonbProperty;
import javax.validation.Valid;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

/**
* Customer Engagement information.
*
*/
@Schema(description = "Customer Engagement information.\n")
@javax.annotation.processing.Generated(value = "dk.mada.jaxrs.Generator")
public class CustomerEngagement {
public static final String JSON_PROPERTY_ROLE_CODE_INT = "roleCodeInt";
@JsonbProperty(JSON_PROPERTY_ROLE_CODE_INT)
private RoleCodeInt roleCodeInt;

public static final String JSON_PROPERTY_ROLE_CODE_LONG = "roleCodeLong";
@JsonbProperty(JSON_PROPERTY_ROLE_CODE_LONG)
private RoleCodeLong roleCodeLong;

public static final String JSON_PROPERTY_ROLE_CODE_SHORT = "roleCodeShort";
@JsonbProperty(JSON_PROPERTY_ROLE_CODE_SHORT)
private RoleCodeShort roleCodeShort;

public CustomerEngagement roleCodeInt(RoleCodeInt roleCodeInt) {
this.roleCodeInt = roleCodeInt;
return this;
}

/**
* Get roleCodeInt
* @return roleCodeInt
**/
@Valid
public RoleCodeInt getRoleCodeInt() {
return roleCodeInt;
}

public void setRoleCodeInt(RoleCodeInt roleCodeInt) {
this.roleCodeInt = roleCodeInt;
}

public CustomerEngagement roleCodeLong(RoleCodeLong roleCodeLong) {
this.roleCodeLong = roleCodeLong;
return this;
}

/**
* Get roleCodeLong
* @return roleCodeLong
**/
@Valid
public RoleCodeLong getRoleCodeLong() {
return roleCodeLong;
}

public void setRoleCodeLong(RoleCodeLong roleCodeLong) {
this.roleCodeLong = roleCodeLong;
}

public CustomerEngagement roleCodeShort(RoleCodeShort roleCodeShort) {
this.roleCodeShort = roleCodeShort;
return this;
}

/**
* Get roleCodeShort
* @return roleCodeShort
**/
@Valid
public RoleCodeShort getRoleCodeShort() {
return roleCodeShort;
}

public void setRoleCodeShort(RoleCodeShort roleCodeShort) {
this.roleCodeShort = roleCodeShort;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CustomerEngagement)) {
return false;
}
CustomerEngagement other = (CustomerEngagement) o;
return Objects.equals(this.roleCodeInt, other.roleCodeInt) &&
Objects.equals(this.roleCodeLong, other.roleCodeLong) &&
Objects.equals(this.roleCodeShort, other.roleCodeShort);
}

@Override
public int hashCode() {
return Objects.hash(roleCodeInt, roleCodeLong, roleCodeShort);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class CustomerEngagement {");
sb.append("\n roleCodeInt: ").append(toIndentedString(roleCodeInt));
sb.append("\n roleCodeLong: ").append(toIndentedString(roleCodeLong));
sb.append("\n roleCodeShort: ").append(toIndentedString(roleCodeShort));
sb.append("\n}");
return sb.toString();
}

private String toIndentedString(Object o) {
return Objects.toString(o).replace("\n", "\n ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* openapi API Title
* openapi API description
*
* The version of the OpenAPI document: openapi API Version
*/

package mada.tests.e2e.dto.enums.integers.dto;

import javax.json.Json;
import javax.json.JsonString;
import javax.json.bind.adapter.JsonbAdapter;
import javax.json.bind.annotation.JsonbTypeAdapter;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

/**
* Role for the customer refnr relation. 1=Owner, 0=Related, Unknown role.
*/
@JsonbTypeAdapter(mada.tests.e2e.dto.enums.integers.dto.RoleCodeInt.RoleCodeIntAdapter.class)
@Schema(enumeration = {"0", "1"}, type = SchemaType.INTEGER, format = "int32", description = "Role for the customer refnr relation. 1=Owner, 0=Related, Unknown role.")
@javax.annotation.processing.Generated(value = "dk.mada.jaxrs.Generator")
public enum RoleCodeInt {
NUMBER_0(0),
NUMBER_1(1);

private final int value;

RoleCodeInt(int value) {
this.value = value;
}

public int getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

public static class RoleCodeIntAdapter implements JsonbAdapter<RoleCodeInt, JsonString> {
@Override
public JsonString adaptToJson(RoleCodeInt e) throws Exception {
return Json.createValue(String.valueOf(e.value));
}

@Override
public RoleCodeInt adaptFromJson(JsonString value) throws Exception {
for (RoleCodeInt b : RoleCodeInt.values()) {
if (String.valueOf(b.value).equalsIgnoreCase(value.getString())) {
return b;
}
}
throw new IllegalStateException("Unable to deserialize '" + value.getString() + "' to type RoleCodeInt");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* openapi API Title
* openapi API description
*
* The version of the OpenAPI document: openapi API Version
*/

package mada.tests.e2e.dto.enums.integers.dto;

import javax.json.Json;
import javax.json.JsonString;
import javax.json.bind.adapter.JsonbAdapter;
import javax.json.bind.annotation.JsonbTypeAdapter;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

/**
* Role for the customer refnr relation. 1=Owner, 0=Related, Unknown role.
*/
@JsonbTypeAdapter(mada.tests.e2e.dto.enums.integers.dto.RoleCodeLong.RoleCodeLongAdapter.class)
@Schema(enumeration = {"0", "1"}, description = "Role for the customer refnr relation. 1=Owner, 0=Related, Unknown role.")
@javax.annotation.processing.Generated(value = "dk.mada.jaxrs.Generator")
public enum RoleCodeLong {
NUMBER_0(0L),
NUMBER_1(1L);

private final long value;

RoleCodeLong(long value) {
this.value = value;
}

public long getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

public static class RoleCodeLongAdapter implements JsonbAdapter<RoleCodeLong, JsonString> {
@Override
public JsonString adaptToJson(RoleCodeLong e) throws Exception {
return Json.createValue(String.valueOf(e.value));
}

@Override
public RoleCodeLong adaptFromJson(JsonString value) throws Exception {
for (RoleCodeLong b : RoleCodeLong.values()) {
if (String.valueOf(b.value).equalsIgnoreCase(value.getString())) {
return b;
}
}
throw new IllegalStateException("Unable to deserialize '" + value.getString() + "' to type RoleCodeLong");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* openapi API Title
* openapi API description
*
* The version of the OpenAPI document: openapi API Version
*/

package mada.tests.e2e.dto.enums.integers.dto;

import javax.json.Json;
import javax.json.JsonString;
import javax.json.bind.adapter.JsonbAdapter;
import javax.json.bind.annotation.JsonbTypeAdapter;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

/**
* Role for the customer refnr relation. 1=Owner, 0=Related, Unknown role.
*/
@JsonbTypeAdapter(mada.tests.e2e.dto.enums.integers.dto.RoleCodeShort.RoleCodeShortAdapter.class)
@Schema(enumeration = {"0", "1"}, description = "Role for the customer refnr relation. 1=Owner, 0=Related, Unknown role.")
@javax.annotation.processing.Generated(value = "dk.mada.jaxrs.Generator")
public enum RoleCodeShort {
NUMBER_0((short)0),
NUMBER_1((short)1);

private final short value;

RoleCodeShort(short value) {
this.value = value;
}

public short getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

public static class RoleCodeShortAdapter implements JsonbAdapter<RoleCodeShort, JsonString> {
@Override
public JsonString adaptToJson(RoleCodeShort e) throws Exception {
return Json.createValue(String.valueOf(e.value));
}

@Override
public RoleCodeShort adaptFromJson(JsonString value) throws Exception {
for (RoleCodeShort b : RoleCodeShort.values()) {
if (String.valueOf(b.value).equalsIgnoreCase(value.getString())) {
return b;
}
}
throw new IllegalStateException("Unable to deserialize '" + value.getString() + "' to type RoleCodeShort");
}
}
}
Loading

0 comments on commit 3ac61fd

Please sign in to comment.