Skip to content

Instantly share code, notes, and snippets.

@msmerc
Last active December 20, 2021 17:08
Show Gist options
  • Save msmerc/bb7d4b848585a26b7baa2bd5722d1fcc to your computer and use it in GitHub Desktop.
Save msmerc/bb7d4b848585a26b7baa2bd5722d1fcc to your computer and use it in GitHub Desktop.
Swagger polymorphic test
openapi: "3.0.0"
info:
description: Test API
version: "2.2.1"
title: "Test API"
servers:
- url: "https://this-is-a-test/eap"
description: "API server"
paths:
/test/datasets/:
get:
tags:
- "dataset"
summary: "Dataset definition and available snapshot resources"
description: blah blah blah
operationId: "getDataset"
parameters:
- $ref: "#/components/parameters/catalog"
- $ref: "#/components/parameters/dataset"
responses:
"200":
description: "Success"
content:
application/ld+json:
schema:
oneOf:
- $ref: "#/components/schemas/Dataset"
- $ref: "#/components/schemas/HistoryDataset"
components:
schemas:
Type:
type: string
description: "JSON-LD type"
externalDocs:
description: "JSON-LD definition"
url: "https://www.w3.org/TR/json-ld/#typed-values"
AssociatedRequest:
type: string
description: blah blah blah
BaseDataset:
type: object
description: "A basic dataset resource."
required:
- "@id"
- "@type"
- "title"
- "description"
- "identifier"
- "contains"
properties:
"@id":
type: string
"@type":
allOf:
- $ref: "#/components/schemas/Type"
- enum:
- "BaseDataset"
"title":
type: string
"description":
type: string
Dataset:
allOf:
- $ref: "#/components/schemas/BaseDataset"
- type: object
description: "A dataset resource."
required:
- "@type"
properties:
"@type":
allOf:
- $ref: "#/components/schemas/Type"
- enum:
- "Dataset"
"request":
$ref: "#/components/schemas/AssociatedRequest"
HistoryDataset:
allOf:
- $ref: "#/components/schemas/BaseDataset"
- type: object
description: blah blah blah
required:
- "@type"
properties:
"@type":
allOf:
- $ref: "#/components/schemas/Type"
- enum:
- "HistoryDataset"
"request":
$ref: "#/components/schemas/AssociatedRequest"
parameters:
catalog:
name: "catalog"
in: "path"
description: blah blah blah
required: true
schema:
type: "string"
dataset:
name: "dataset"
in: "path"
description: "Dataset identifier"
required: true
schema:
type: "string"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>swagger-dev-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<swagger-core-version>2.1.11</swagger-core-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.12.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.annotation</artifactId>
<version>3.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.30</version>
<dependencies>
<dependency>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-cli</artifactId>
<!-- This is the custom JAR you gave me! -->
<version>poly-SNAPSHOT</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-model</id>
<!-- This generates only the models, because we want to use jersey -->
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<language>java</language>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<library>jersey2</library>
<apiPackage>com.testing.api</apiPackage>
<modelPackage>com.testing.model</modelPackage>
<java11>true</java11>
<dateLibrary>java8</dateLibrary>
</configOptions>
<generateModels>true</generateModels>
<generateApis>false</generateApis>
<generateModelDocumentation>false</generateModelDocumentation>
<generateSupportingFiles>false</generateSupportingFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.testing.model.Dataset;
import com.testing.model.OneOfinlineResponse200;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class TestDataset {
@Test
public void testDataset() throws JsonProcessingException {
Dataset dataset = new Dataset();
dataset.setDatasetAtType("Dataset"); //Mandated by the spec, but whatever.
dataset.setRequest("request");
dataset.setAtId("id");
//This is impossible:
//dataset.setAtType(new AnyOfBaseDatasetAtType() {});
ObjectMapper om = new ObjectMapper();
String val = om.writeValueAsString(dataset);
OneOfinlineResponse200 response200 = om.readValue(val, OneOfinlineResponse200.class);
Assertions.assertEquals(dataset, response200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment