use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.
the class ReaderTest method testSingleExample.
@Test(description = "Single Example")
public void testSingleExample() {
Reader reader = new Reader(new OpenAPI());
OpenAPI openAPI = reader.read(SingleExampleResource.class);
String yaml = "openapi: 3.0.1\n" + "paths:\n" + " /test1:\n" + " post:\n" + " operationId: test1\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/User'\n" + " example:\n" + " foo: foo\n" + " bar: bar\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*': {}\n" + " /test2:\n" + " post:\n" + " operationId: test2\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/User'\n" + " example:\n" + " foo: foo\n" + " bar: bar\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*': {}\n" + "components:\n" + " schemas:\n" + " User:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " username:\n" + " type: string\n" + " firstName:\n" + " type: string\n" + " lastName:\n" + " type: string\n" + " email:\n" + " type: string\n" + " password:\n" + " type: string\n" + " phone:\n" + " type: string\n" + " userStatus:\n" + " type: integer\n" + " description: User Status\n" + " format: int32\n" + " xml:\n" + " name: User\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.
the class ReaderTest method testTicket2806.
@Test(description = "array schema example")
public void testTicket2806() {
Reader reader = new Reader(new OpenAPI());
OpenAPI openAPI = reader.read(Ticket2806Resource.class);
String yaml = "openapi: 3.0.1\n" + "paths:\n" + " /test:\n" + " get:\n" + " operationId: getTest\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/Test'\n" + "components:\n" + " schemas:\n" + " Test:\n" + " type: object\n" + " properties:\n" + " stringArray:\n" + " maxItems: 4\n" + " minItems: 2\n" + " uniqueItems: true\n" + " type: array\n" + " description: Array desc\n" + " example:\n" + " - aaa\n" + " - bbb\n" + " items:\n" + " type: string\n" + " description: Hello, World!\n" + " example: Lorem ipsum dolor set\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.
the class ReaderTest method testTicket3029.
@Test(description = "Parameter with ref")
public void testTicket3029() {
Components components = new Components();
components.addParameters("id", new Parameter().description("Id Description").schema(new IntegerSchema()).in(ParameterIn.QUERY.toString()).example(1).required(true));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(RefParameter3029Resource.class);
String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /2:\n" + " get:\n" + " summary: Simple get operation\n" + " operationId: sendPayload2\n" + " parameters:\n" + " - $ref: '#/components/parameters/id'\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*': {}\n" + " /1:\n" + " get:\n" + " summary: Simple get operation\n" + " operationId: sendPayload1\n" + " parameters:\n" + " - $ref: '#/components/parameters/id'\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*': {}\n" + "components:\n" + " parameters:\n" + " id:\n" + " in: query\n" + " description: Id Description\n" + " required: true\n" + " schema:\n" + " type: integer\n" + " format: int32\n" + " example: 1\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.
the class ModelResolver method resolveSchemaMembers.
protected void resolveSchemaMembers(Schema schema, Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schemaAnnotation) {
String description = resolveDescription(a, annotations, schemaAnnotation);
if (StringUtils.isNotBlank(description)) {
schema.description(description);
}
String title = resolveTitle(a, annotations, schemaAnnotation);
if (StringUtils.isNotBlank(title)) {
schema.title(title);
}
String format = resolveFormat(a, annotations, schemaAnnotation);
if (StringUtils.isNotBlank(format) && StringUtils.isBlank(schema.getFormat())) {
schema.format(format);
}
Object defaultValue = resolveDefaultValue(a, annotations, schemaAnnotation);
if (defaultValue != null) {
schema.setDefault(defaultValue);
}
Object example = resolveExample(a, annotations, schemaAnnotation);
if (example != null) {
schema.example(example);
}
Boolean readOnly = resolveReadOnly(a, annotations, schemaAnnotation);
if (readOnly != null) {
schema.readOnly(readOnly);
}
Boolean nullable = resolveNullable(a, annotations, schemaAnnotation);
if (nullable != null) {
schema.nullable(nullable);
}
BigDecimal multipleOf = resolveMultipleOf(a, annotations, schemaAnnotation);
if (multipleOf != null) {
schema.multipleOf(multipleOf);
}
Integer maxLength = resolveMaxLength(a, annotations, schemaAnnotation);
if (maxLength != null) {
schema.maxLength(maxLength);
}
Integer minLength = resolveMinLength(a, annotations, schemaAnnotation);
if (minLength != null) {
schema.minLength(minLength);
}
BigDecimal minimum = resolveMinimum(a, annotations, schemaAnnotation);
if (minimum != null) {
schema.minimum(minimum);
}
BigDecimal maximum = resolveMaximum(a, annotations, schemaAnnotation);
if (maximum != null) {
schema.maximum(maximum);
}
Boolean exclusiveMinimum = resolveExclusiveMinimum(a, annotations, schemaAnnotation);
if (exclusiveMinimum != null) {
schema.exclusiveMinimum(exclusiveMinimum);
}
Boolean exclusiveMaximum = resolveExclusiveMaximum(a, annotations, schemaAnnotation);
if (exclusiveMaximum != null) {
schema.exclusiveMaximum(exclusiveMaximum);
}
String pattern = resolvePattern(a, annotations, schemaAnnotation);
if (StringUtils.isNotBlank(pattern)) {
schema.pattern(pattern);
}
Integer minProperties = resolveMinProperties(a, annotations, schemaAnnotation);
if (minProperties != null) {
schema.minProperties(minProperties);
}
Integer maxProperties = resolveMaxProperties(a, annotations, schemaAnnotation);
if (maxProperties != null) {
schema.maxProperties(maxProperties);
}
List<String> requiredProperties = resolveRequiredProperties(a, annotations, schemaAnnotation);
if (requiredProperties != null) {
for (String prop : requiredProperties) {
addRequiredItem(schema, prop);
}
}
Boolean writeOnly = resolveWriteOnly(a, annotations, schemaAnnotation);
if (writeOnly != null) {
schema.writeOnly(writeOnly);
}
ExternalDocumentation externalDocs = resolveExternalDocumentation(a, annotations, schemaAnnotation);
if (externalDocs != null) {
schema.externalDocs(externalDocs);
}
Boolean deprecated = resolveDeprecated(a, annotations, schemaAnnotation);
if (deprecated != null) {
schema.deprecated(deprecated);
}
List<String> allowableValues = resolveAllowableValues(a, annotations, schemaAnnotation);
if (allowableValues != null) {
for (String prop : allowableValues) {
schema.addEnumItemObject(prop);
}
}
Map<String, Object> extensions = resolveExtensions(a, annotations, schemaAnnotation);
if (extensions != null) {
extensions.forEach(schema::addExtension);
}
}
use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.
the class AnnotationsUtils method resolveExample.
private static boolean resolveExample(Example exampleObject, ExampleObject example) {
boolean isEmpty = true;
if (StringUtils.isNotBlank(example.summary())) {
isEmpty = false;
exampleObject.setSummary(example.summary());
}
if (StringUtils.isNotBlank(example.description())) {
isEmpty = false;
exampleObject.setDescription(example.description());
}
if (StringUtils.isNotBlank(example.externalValue())) {
isEmpty = false;
exampleObject.setExternalValue(example.externalValue());
}
if (StringUtils.isNotBlank(example.value())) {
isEmpty = false;
try {
ObjectMapper mapper = ObjectMapperFactory.buildStrictGenericObjectMapper();
exampleObject.setValue(mapper.readTree(example.value()));
} catch (IOException e) {
exampleObject.setValue(example.value());
}
}
if (StringUtils.isNotBlank(example.ref())) {
isEmpty = false;
exampleObject.set$ref(example.ref());
}
if (example.extensions().length > 0) {
isEmpty = false;
Map<String, Object> extensions = AnnotationsUtils.getExtensions(example.extensions());
if (extensions != null) {
extensions.forEach(exampleObject::addExtension);
}
}
return !isEmpty;
}
Aggregations