Created
February 3, 2026 09:42
-
-
Save jmspiewak/373d307906e3233d3975496f4ba6a647 to your computer and use it in GitHub Desktop.
Inline response objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| openapi: 3.0.1 | |
| info: | |
| version: 1.0.0 | |
| title: Example | |
| license: | |
| name: MIT | |
| servers: | |
| - url: http://api.example.xyz/v1 | |
| paths: | |
| /meta: | |
| get: | |
| operationId: meta | |
| responses: | |
| '200': | |
| description: OK | |
| content: | |
| application/json: | |
| schema: | |
| type: object | |
| properties: | |
| name: | |
| type: string | |
| id: | |
| type: integer | |
| '403': | |
| description: Error | |
| content: | |
| application/json: | |
| schema: | |
| type: object | |
| properties: | |
| message: | |
| type: string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Example | |
| * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | |
| * | |
| * The version of the OpenAPI document: 1.0.0 | |
| * | |
| * | |
| * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | |
| * https://openapi-generator.tech | |
| * Do not edit the class manually. | |
| */ | |
| package test; | |
| import java.util.Objects; | |
| import com.google.gson.TypeAdapter; | |
| import com.google.gson.annotations.JsonAdapter; | |
| import com.google.gson.annotations.SerializedName; | |
| import com.google.gson.stream.JsonReader; | |
| import com.google.gson.stream.JsonWriter; | |
| import java.io.IOException; | |
| import java.util.Arrays; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| import com.google.gson.JsonArray; | |
| import com.google.gson.JsonDeserializationContext; | |
| import com.google.gson.JsonDeserializer; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| import com.google.gson.JsonParseException; | |
| import com.google.gson.TypeAdapterFactory; | |
| import com.google.gson.reflect.TypeToken; | |
| import com.google.gson.TypeAdapter; | |
| import com.google.gson.stream.JsonReader; | |
| import com.google.gson.stream.JsonWriter; | |
| import java.io.IOException; | |
| import java.util.HashMap; | |
| import java.util.HashSet; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Set; | |
| import invalidPackageName.JSON; | |
| /** | |
| * Meta200Response | |
| */ | |
| @jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.19.0") | |
| public class Meta200Response { | |
| public static final String SERIALIZED_NAME_NAME = "name"; | |
| @SerializedName(SERIALIZED_NAME_NAME) | |
| @jakarta.annotation.Nullable | |
| private String name; | |
| public static final String SERIALIZED_NAME_ID = "id"; | |
| @SerializedName(SERIALIZED_NAME_ID) | |
| @jakarta.annotation.Nullable | |
| private Integer id; | |
| public Meta200Response() { | |
| } | |
| public Meta200Response name(@jakarta.annotation.Nullable String name) { | |
| this.name = name; | |
| return this; | |
| } | |
| /** | |
| * Get name | |
| * @return name | |
| */ | |
| @jakarta.annotation.Nullable | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(@jakarta.annotation.Nullable String name) { | |
| this.name = name; | |
| } | |
| public Meta200Response id(@jakarta.annotation.Nullable Integer id) { | |
| this.id = id; | |
| return this; | |
| } | |
| /** | |
| * Get id | |
| * @return id | |
| */ | |
| @jakarta.annotation.Nullable | |
| public Integer getId() { | |
| return id; | |
| } | |
| public void setId(@jakarta.annotation.Nullable Integer id) { | |
| this.id = id; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| if (this == o) { | |
| return true; | |
| } | |
| if (o == null || getClass() != o.getClass()) { | |
| return false; | |
| } | |
| Meta200Response meta200Response = (Meta200Response) o; | |
| return Objects.equals(this.name, meta200Response.name) && | |
| Objects.equals(this.id, meta200Response.id); | |
| } | |
| @Override | |
| public int hashCode() { | |
| return Objects.hash(name, id); | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append("class Meta200Response {\n"); | |
| sb.append(" name: ").append(toIndentedString(name)).append("\n"); | |
| sb.append(" id: ").append(toIndentedString(id)).append("\n"); | |
| sb.append("}"); | |
| return sb.toString(); | |
| } | |
| /** | |
| * Convert the given object to string with each line indented by 4 spaces | |
| * (except the first line). | |
| */ | |
| private String toIndentedString(Object o) { | |
| if (o == null) { | |
| return "null"; | |
| } | |
| return o.toString().replace("\n", "\n "); | |
| } | |
| public static HashSet<String> openapiFields; | |
| public static HashSet<String> openapiRequiredFields; | |
| static { | |
| // a set of all properties/fields (JSON key names) | |
| openapiFields = new HashSet<String>(Arrays.asList("name", "id")); | |
| // a set of required properties/fields (JSON key names) | |
| openapiRequiredFields = new HashSet<String>(0); | |
| } | |
| /** | |
| * Validates the JSON Element and throws an exception if issues found | |
| * | |
| * @param jsonElement JSON Element | |
| * @throws IOException if the JSON Element is invalid with respect to Meta200Response | |
| */ | |
| public static void validateJsonElement(JsonElement jsonElement) throws IOException { | |
| if (jsonElement == null) { | |
| if (!Meta200Response.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null | |
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The required field(s) %s in Meta200Response is not found in the empty JSON string", Meta200Response.openapiRequiredFields.toString())); | |
| } | |
| } | |
| Set<Map.Entry<String, JsonElement>> entries = jsonElement.getAsJsonObject().entrySet(); | |
| // check to see if the JSON string contains additional fields | |
| for (Map.Entry<String, JsonElement> entry : entries) { | |
| if (!Meta200Response.openapiFields.contains(entry.getKey())) { | |
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The field `%s` in the JSON string is not defined in the `Meta200Response` properties. JSON: %s", entry.getKey(), jsonElement.toString())); | |
| } | |
| } | |
| JsonObject jsonObj = jsonElement.getAsJsonObject(); | |
| if ((jsonObj.get("name") != null && !jsonObj.get("name").isJsonNull()) && !jsonObj.get("name").isJsonPrimitive()) { | |
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `name` to be a primitive type in the JSON string but got `%s`", jsonObj.get("name").toString())); | |
| } | |
| } | |
| public static class CustomTypeAdapterFactory implements TypeAdapterFactory { | |
| @SuppressWarnings("unchecked") | |
| @Override | |
| public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | |
| if (!Meta200Response.class.isAssignableFrom(type.getRawType())) { | |
| return null; // this class only serializes 'Meta200Response' and its subtypes | |
| } | |
| final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); | |
| final TypeAdapter<Meta200Response> thisAdapter | |
| = gson.getDelegateAdapter(this, TypeToken.get(Meta200Response.class)); | |
| return (TypeAdapter<T>) new TypeAdapter<Meta200Response>() { | |
| @Override | |
| public void write(JsonWriter out, Meta200Response value) throws IOException { | |
| JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject(); | |
| elementAdapter.write(out, obj); | |
| } | |
| @Override | |
| public Meta200Response read(JsonReader in) throws IOException { | |
| JsonElement jsonElement = elementAdapter.read(in); | |
| validateJsonElement(jsonElement); | |
| return thisAdapter.fromJsonTree(jsonElement); | |
| } | |
| }.nullSafe(); | |
| } | |
| } | |
| /** | |
| * Create an instance of Meta200Response given an JSON string | |
| * | |
| * @param jsonString JSON string | |
| * @return An instance of Meta200Response | |
| * @throws IOException if the JSON string is invalid with respect to Meta200Response | |
| */ | |
| public static Meta200Response fromJson(String jsonString) throws IOException { | |
| return JSON.getGson().fromJson(jsonString, Meta200Response.class); | |
| } | |
| /** | |
| * Convert an instance of Meta200Response to an JSON string | |
| * | |
| * @return JSON string | |
| */ | |
| public String toJson() { | |
| return JSON.getGson().toJson(this); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Example | |
| * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | |
| * | |
| * The version of the OpenAPI document: 1.0.0 | |
| * | |
| * | |
| * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | |
| * https://openapi-generator.tech | |
| * Do not edit the class manually. | |
| */ | |
| package test; | |
| import java.util.Objects; | |
| import com.google.gson.TypeAdapter; | |
| import com.google.gson.annotations.JsonAdapter; | |
| import com.google.gson.annotations.SerializedName; | |
| import com.google.gson.stream.JsonReader; | |
| import com.google.gson.stream.JsonWriter; | |
| import java.io.IOException; | |
| import java.util.Arrays; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| import com.google.gson.JsonArray; | |
| import com.google.gson.JsonDeserializationContext; | |
| import com.google.gson.JsonDeserializer; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| import com.google.gson.JsonParseException; | |
| import com.google.gson.TypeAdapterFactory; | |
| import com.google.gson.reflect.TypeToken; | |
| import com.google.gson.TypeAdapter; | |
| import com.google.gson.stream.JsonReader; | |
| import com.google.gson.stream.JsonWriter; | |
| import java.io.IOException; | |
| import java.util.HashMap; | |
| import java.util.HashSet; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Set; | |
| import invalidPackageName.JSON; | |
| /** | |
| * Meta403Response | |
| */ | |
| @jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.19.0") | |
| public class Meta403Response { | |
| public static final String SERIALIZED_NAME_MESSAGE = "message"; | |
| @SerializedName(SERIALIZED_NAME_MESSAGE) | |
| @jakarta.annotation.Nullable | |
| private String message; | |
| public Meta403Response() { | |
| } | |
| public Meta403Response message(@jakarta.annotation.Nullable String message) { | |
| this.message = message; | |
| return this; | |
| } | |
| /** | |
| * Get message | |
| * @return message | |
| */ | |
| @jakarta.annotation.Nullable | |
| public String getMessage() { | |
| return message; | |
| } | |
| public void setMessage(@jakarta.annotation.Nullable String message) { | |
| this.message = message; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| if (this == o) { | |
| return true; | |
| } | |
| if (o == null || getClass() != o.getClass()) { | |
| return false; | |
| } | |
| Meta403Response meta403Response = (Meta403Response) o; | |
| return Objects.equals(this.message, meta403Response.message); | |
| } | |
| @Override | |
| public int hashCode() { | |
| return Objects.hash(message); | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append("class Meta403Response {\n"); | |
| sb.append(" message: ").append(toIndentedString(message)).append("\n"); | |
| sb.append("}"); | |
| return sb.toString(); | |
| } | |
| /** | |
| * Convert the given object to string with each line indented by 4 spaces | |
| * (except the first line). | |
| */ | |
| private String toIndentedString(Object o) { | |
| if (o == null) { | |
| return "null"; | |
| } | |
| return o.toString().replace("\n", "\n "); | |
| } | |
| public static HashSet<String> openapiFields; | |
| public static HashSet<String> openapiRequiredFields; | |
| static { | |
| // a set of all properties/fields (JSON key names) | |
| openapiFields = new HashSet<String>(Arrays.asList("message")); | |
| // a set of required properties/fields (JSON key names) | |
| openapiRequiredFields = new HashSet<String>(0); | |
| } | |
| /** | |
| * Validates the JSON Element and throws an exception if issues found | |
| * | |
| * @param jsonElement JSON Element | |
| * @throws IOException if the JSON Element is invalid with respect to Meta403Response | |
| */ | |
| public static void validateJsonElement(JsonElement jsonElement) throws IOException { | |
| if (jsonElement == null) { | |
| if (!Meta403Response.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null | |
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The required field(s) %s in Meta403Response is not found in the empty JSON string", Meta403Response.openapiRequiredFields.toString())); | |
| } | |
| } | |
| Set<Map.Entry<String, JsonElement>> entries = jsonElement.getAsJsonObject().entrySet(); | |
| // check to see if the JSON string contains additional fields | |
| for (Map.Entry<String, JsonElement> entry : entries) { | |
| if (!Meta403Response.openapiFields.contains(entry.getKey())) { | |
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The field `%s` in the JSON string is not defined in the `Meta403Response` properties. JSON: %s", entry.getKey(), jsonElement.toString())); | |
| } | |
| } | |
| JsonObject jsonObj = jsonElement.getAsJsonObject(); | |
| if ((jsonObj.get("message") != null && !jsonObj.get("message").isJsonNull()) && !jsonObj.get("message").isJsonPrimitive()) { | |
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `message` to be a primitive type in the JSON string but got `%s`", jsonObj.get("message").toString())); | |
| } | |
| } | |
| public static class CustomTypeAdapterFactory implements TypeAdapterFactory { | |
| @SuppressWarnings("unchecked") | |
| @Override | |
| public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | |
| if (!Meta403Response.class.isAssignableFrom(type.getRawType())) { | |
| return null; // this class only serializes 'Meta403Response' and its subtypes | |
| } | |
| final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); | |
| final TypeAdapter<Meta403Response> thisAdapter | |
| = gson.getDelegateAdapter(this, TypeToken.get(Meta403Response.class)); | |
| return (TypeAdapter<T>) new TypeAdapter<Meta403Response>() { | |
| @Override | |
| public void write(JsonWriter out, Meta403Response value) throws IOException { | |
| JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject(); | |
| elementAdapter.write(out, obj); | |
| } | |
| @Override | |
| public Meta403Response read(JsonReader in) throws IOException { | |
| JsonElement jsonElement = elementAdapter.read(in); | |
| validateJsonElement(jsonElement); | |
| return thisAdapter.fromJsonTree(jsonElement); | |
| } | |
| }.nullSafe(); | |
| } | |
| } | |
| /** | |
| * Create an instance of Meta403Response given an JSON string | |
| * | |
| * @param jsonString JSON string | |
| * @return An instance of Meta403Response | |
| * @throws IOException if the JSON string is invalid with respect to Meta403Response | |
| */ | |
| public static Meta403Response fromJson(String jsonString) throws IOException { | |
| return JSON.getGson().fromJson(jsonString, Meta403Response.class); | |
| } | |
| /** | |
| * Convert an instance of Meta403Response to an JSON string | |
| * | |
| * @return JSON string | |
| */ | |
| public String toJson() { | |
| return JSON.getGson().toJson(this); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment