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

addd flag for media type example set value #3975

Merged
merged 3 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,23 @@
package io.swagger.v3.core.jackson.mixin;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.Map;

public abstract class ExampleMixin {

@JsonAnyGetter
public abstract Map<String, Object> getExtensions();

@JsonAnySetter
public abstract void addExtension(String name, Object value);

@JsonInclude(JsonInclude.Include.CUSTOM)
public abstract Object getValue();

@JsonIgnore
public abstract boolean getValueSetFlag();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.swagger.v3.core.jackson.mixin;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.Map;

public abstract class MediaTypeMixin {

@JsonAnyGetter
public abstract Map<String, Object> getExtensions();

@JsonAnySetter
public abstract void addExtension(String name, Object value);

@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonInclude(JsonInclude.Include.CUSTOM)
public abstract Object getExample();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import io.swagger.v3.core.jackson.SchemaSerializer;
import io.swagger.v3.core.jackson.mixin.ComponentsMixin;
import io.swagger.v3.core.jackson.mixin.DateSchemaMixin;
import io.swagger.v3.core.jackson.mixin.ExampleMixin;
import io.swagger.v3.core.jackson.mixin.ExtensionsMixin;
import io.swagger.v3.core.jackson.mixin.MediaTypeMixin;
import io.swagger.v3.core.jackson.mixin.OpenAPIMixin;
import io.swagger.v3.core.jackson.mixin.OperationMixin;
import io.swagger.v3.core.jackson.mixin.SchemaMixin;
Expand Down Expand Up @@ -107,14 +109,14 @@ public JsonSerializer<?> modifySerializer(
sourceMixins.put(Contact.class, ExtensionsMixin.class);
sourceMixins.put(Encoding.class, ExtensionsMixin.class);
sourceMixins.put(EncodingProperty.class, ExtensionsMixin.class);
sourceMixins.put(Example.class, ExtensionsMixin.class);
sourceMixins.put(Example.class, ExampleMixin.class);
sourceMixins.put(ExternalDocumentation.class, ExtensionsMixin.class);
sourceMixins.put(Header.class, ExtensionsMixin.class);
sourceMixins.put(Info.class, ExtensionsMixin.class);
sourceMixins.put(License.class, ExtensionsMixin.class);
sourceMixins.put(Link.class, ExtensionsMixin.class);
sourceMixins.put(LinkParameter.class, ExtensionsMixin.class);
sourceMixins.put(MediaType.class, ExtensionsMixin.class);
sourceMixins.put(MediaType.class, MediaTypeMixin.class);
sourceMixins.put(OAuthFlow.class, ExtensionsMixin.class);
sourceMixins.put(OAuthFlows.class, ExtensionsMixin.class);
sourceMixins.put(OpenAPI.class, OpenAPIMixin.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.commons.io.FileUtils;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -409,4 +411,20 @@ public void testNullExampleDeserialization() throws Exception {
Yaml.prettyPrint(oas);
}

@Test
public void testExampleDeserializationOnMediaType() throws Exception {
String content = FileUtils.readFileToString(new File("src/test/resources/specFiles/media-type-null-example.yaml"), "UTF-8");
OpenAPI openAPI = Yaml.mapper().readValue(content, OpenAPI.class);

assertNull(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExample());
assertTrue(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExampleSetFlag());

assertNull(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExample());
assertFalse(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExampleSetFlag());

assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample());

assertTrue(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExampleSetFlag());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
openapi: "3.0.1"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: https://petstore3.swagger.io/api/v3
paths:
/pet:
post:
tags:
- pet
summary: Add a new pet to the store
operationId: addPet
requestBody:
description: Pet object that needs to be added to the store
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
example:
id: 10
name: kitty
tag: something
required: true
responses:
200:
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
200:
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
example: null
components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Example {
private String $ref = null;
private java.util.Map<String, Object> extensions = null;

private boolean valueSetFlag;

/**
* returns the summary property from a Example instance.
*
Expand Down Expand Up @@ -78,10 +80,11 @@ public Object getValue() {

public void setValue(Object value) {
this.value = value;
valueSetFlag = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see how this is done in Schema to correctly handle all scenarios

}

public Example value(Object value) {
this.value = value;
setValue(value);
return this;
}

Expand Down Expand Up @@ -143,6 +146,14 @@ public Example extensions(java.util.Map<String, Object> extensions) {
return this;
}

public boolean getValueSetFlag() {
return valueSetFlag;
}

public void setValueSetFlag(boolean valueSetFlag) {
this.valueSetFlag = valueSetFlag;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class MediaType {
private Map<String, Encoding> encoding = null;
private java.util.Map<String, Object> extensions = null;

private boolean exampleSetFlag;

/**
* returns the schema property from a MediaType instance.
*
Expand Down Expand Up @@ -93,10 +95,11 @@ public Object getExample() {

public void setExample(Object example) {
this.example = example;
exampleSetFlag = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see how this is done in Schema to correctly handle all scenarios

}

public MediaType example(Object example) {
this.example = example;
setExample(example);
return this;
}

Expand Down Expand Up @@ -127,6 +130,14 @@ public MediaType addEncoding(String key, Encoding encodingItem) {
return this;
}

public boolean getExampleSetFlag() {
return exampleSetFlag;
}

public void setExampleSetFlag(boolean exampleSetFlag) {
this.exampleSetFlag = exampleSetFlag;
}

@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
Expand Down