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

oas 3.1 - opinionated deterministic schema properties ordering #4150

Merged
merged 1 commit into from
Mar 25, 2022
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 @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand All @@ -15,6 +16,7 @@
import java.util.Map;
import java.util.Set;

@JsonPropertyOrder(value = {"type", "format", "if", "then", "else"}, alphabetic = true)
public abstract class Schema31Mixin {

//@JsonValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,79 @@
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.JsonSchema;
import org.testng.annotations.Test;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;

public class SchemaSerializationTest {

@Test
public void serializeRefSchema3_1() {
OpenAPI openAPI = new OpenAPI()
.components(new Components()
.addSchemas("Pet", new Schema()
.addProperties("id", new Schema().type("integer"))
.addProperties("name", new Schema().type("string"))
.addProperties("tag", new Schema().type("string")))
.addSchemas("AnotherPet", new Schema()
.addSchemas("Pet", new JsonSchema()
.types(new HashSet<>(Arrays.asList("object")))
.format("whatever")
._if(new JsonSchema().type("string").types(new HashSet<>(Arrays.asList("string"))))
.then(new JsonSchema().type("string").types(new HashSet<>(Arrays.asList("string"))))
._else(new JsonSchema().type("string").types(new HashSet<>(Arrays.asList("string"))))
.minimum(BigDecimal.valueOf(1))
.addProperties("id", new JsonSchema().type("integer").types(new HashSet<>(Arrays.asList("integer"))))
.addProperties("name", new JsonSchema().type("string").types(new HashSet<>(Arrays.asList("string"))))
.addProperties("tag", new JsonSchema().type("string").types(new HashSet<>(Arrays.asList("string")))))
.addSchemas("AnotherPet", new JsonSchema()
.title("Another Pet")
.description("Another Pet for petstore referencing Pet schema")
.$ref("#/components/schemas/Pet")
.addProperties("category", new Schema().type("string"))
.addProperties("photoUrl", new Schema().type("string"))));
.addProperties("category", new JsonSchema().types(new HashSet<>(Arrays.asList("string"))))
.addProperties("photoUrl", new JsonSchema().types(new HashSet<>(Arrays.asList("string"))))));

SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.0.1\n" +
"components:\n" +
" schemas:\n" +
" Pet:\n" +
" type: object\n" +
" format: whatever\n" +
" if:\n" +
" type: string\n" +
" then:\n" +
" type: string\n" +
" else:\n" +
" type: string\n" +
" minimum: 1\n" +
" properties:\n" +
" id: {}\n" +
" name: {}\n" +
" tag: {}\n" +
" id:\n" +
" type: integer\n" +
" name:\n" +
" type: string\n" +
" tag:\n" +
" type: string\n" +
" AnotherPet:\n" +
" title: Another Pet\n" +
" properties:\n" +
" category: {}\n" +
" photoUrl: {}\n" +
" $ref: '#/components/schemas/Pet'\n" +
" description: Another Pet for petstore referencing Pet schema\n" +
" $ref: '#/components/schemas/Pet'");
" properties:\n" +
" category:\n" +
" type: string\n" +
" photoUrl:\n" +
" type: string\n" +
" title: Another Pet\n");
SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.1\n" +
"components:\n" +
" schemas:\n" +
" Pet:\n" +
" minimum: 1\n" +
" properties:\n" +
" id:\n" +
" type: integer\n" +
" name:\n" +
" type: string\n" +
" tag:\n" +
" type: string\n" +
" format: whatever\n" +
" AnotherPet:\n" +
" $ref: '#/components/schemas/Pet'");
" $ref: '#/components/schemas/Pet'\n");
}
}