Skip to content

Commit

Permalink
oas 3.1 - opinionated deterministic schema properties ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Mar 25, 2022
1 parent 0d21afc commit 636cff7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
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");
}
}

0 comments on commit 636cff7

Please sign in to comment.