use of org.eclipse.microprofile.openapi.models.media.Discriminator in project Payara by payara.
the class MediaBuilderTest method setupBaseDocument.
@Override
protected void setupBaseDocument(OpenAPI document) {
document.getComponents().addSchema("SimpleMap", createSchema().type(SchemaType.OBJECT).additionalPropertiesSchema(createSchema().type(SchemaType.STRING)));
XML xml = createXML().name("name").namespace("namespace").prefix("prefix").attribute(true).wrapped(true).addExtension("x-ext", "ext-value");
document.getComponents().addSchema("XML", createSchema().xml(xml));
Encoding encoding = createEncoding().contentType("contentType").style(Style.FORM).explode(true).allowReserved(true).addExtension("x-ext", "ext-value").addHeader("header1", createHeader().ref("ref1")).addHeader("header2", createHeader().ref("ref2"));
MediaType mediaType = createMediaType().example("example").schema(createSchema().ref("ref")).addExtension("x-ext", "ext-value").addExample("example1", createExample().ref("ref1")).addExample("example2", createExample().ref("ref2")).addEncoding("encoding1", encoding);
document.getComponents().addResponse("MediaType", createAPIResponse().description("description").content(createContent().addMediaType("type1", mediaType)));
Discriminator discriminator = createDiscriminator().propertyName("propertyName").addMapping("key1", "value1").addMapping("key2", "value2");
document.getComponents().addSchema("Discriminator", createSchema().discriminator(discriminator));
document.getComponents().addSchema("Schema", createSchema().title("title").multipleOf(BigDecimal.ONE).maximum(BigDecimal.ONE).exclusiveMaximum(true).minimum(BigDecimal.ONE).exclusiveMinimum(true).maxLength(10).minLength(1).pattern("pattern").maxItems(11).minItems(2).uniqueItems(true).maxProperties(12).minProperties(3).addRequired("required1").addRequired("required2").type(SchemaType.NUMBER).not(createSchema().ref("not")).addProperty("property1", createSchema().ref("property1")).description("description").format("format").nullable(true).readOnly(true).writeOnly(true).example("example").externalDocs(createExternalDocumentation().url("url")).deprecated(true).xml(xml).addEnumeration("enumeration1").addEnumeration("enumeration2").discriminator(discriminator).addAnyOf(createSchema().ref("anyOf1")).addAnyOf(createSchema().ref("anyOf2")).addAllOf(createSchema().ref("allOf1")).addAllOf(createSchema().ref("allOf2")).addOneOf(createSchema().ref("oneOf1")).addOneOf(createSchema().ref("oneOf2")).additionalPropertiesBoolean(true).items(createSchema().ref("items")).addExtension("x-ext", "ext-value"));
}
use of org.eclipse.microprofile.openapi.models.media.Discriminator in project Payara by payara.
the class SchemaImpl method merge.
public static void merge(Schema from, Schema to, boolean override, ApiContext context) {
if (from == null) {
return;
}
if (from.getRef() != null && !from.getRef().isEmpty()) {
applyReference(to, from.getRef());
return;
}
if (from.getType() != null) {
to.setType(mergeProperty(to.getType(), from.getType(), override));
}
if (from instanceof SchemaImpl && to instanceof SchemaImpl) {
final String fromImplementation = ((SchemaImpl) from).getImplementation();
if (fromImplementation != null) {
setImplementation((SchemaImpl) to, fromImplementation, override, context);
}
}
to.setDefaultValue(mergeProperty(to.getDefaultValue(), from.getDefaultValue(), override));
to.setTitle(mergeProperty(to.getTitle(), from.getTitle(), override));
if (from.getMultipleOf() != null && from.getMultipleOf().compareTo(BigDecimal.ZERO) > 0) {
to.setMultipleOf(mergeProperty(to.getMultipleOf(), from.getMultipleOf().stripTrailingZeros(), override));
}
if (from.getMaximum() != null) {
to.setMaximum(mergeProperty(to.getMaximum(), from.getMaximum(), override));
}
to.setExclusiveMaximum(mergeProperty(to.getExclusiveMaximum(), from.getExclusiveMaximum(), override));
if (from.getMinimum() != null) {
to.setMinimum(mergeProperty(to.getMinimum(), from.getMinimum(), override));
}
to.setExclusiveMinimum(mergeProperty(to.getExclusiveMinimum(), from.getExclusiveMinimum(), override));
to.setMaxLength(mergeProperty(to.getMaxLength(), from.getMaxLength(), override));
to.setMinLength(mergeProperty(to.getMinLength(), from.getMinLength(), override));
to.setPattern(mergeProperty(to.getPattern(), from.getPattern(), override));
to.setMaxItems(mergeProperty(to.getMaxItems(), from.getMaxItems(), override));
to.setMinItems(mergeProperty(to.getMinItems(), from.getMinItems(), override));
to.setUniqueItems(mergeProperty(to.getUniqueItems(), from.getUniqueItems(), override));
to.setMaxProperties(mergeProperty(to.getMaxProperties(), from.getMaxProperties(), override));
to.setMinProperties(mergeProperty(to.getMinProperties(), from.getMinProperties(), override));
if (from.getRequired() != null && !from.getRequired().isEmpty()) {
if (to.getRequired() == null) {
to.setRequired(createList());
}
for (String value : from.getRequired()) {
if (!to.getRequired().contains(value)) {
to.addRequired(value);
}
}
}
if (from.getProperties() != null && !from.getProperties().isEmpty()) {
if (to.getProperties() == null) {
to.setProperties(createMap());
}
final Map<String, Schema> toProperties = to.getProperties();
for (Entry<String, Schema> fromEntry : from.getProperties().entrySet()) {
final String name = fromEntry.getKey();
final Schema fromSchema = fromEntry.getValue();
if (!toProperties.containsKey(name)) {
to.addProperty(name, fromSchema);
} else {
final Schema toSchema = toProperties.get(name);
SchemaImpl.merge(fromSchema, toSchema, override, context);
}
}
}
to.setDescription(mergeProperty(to.getDescription(), from.getDescription(), override));
to.setFormat(mergeProperty(to.getFormat(), from.getFormat(), override));
to.setNullable(mergeProperty(to.getNullable(), from.getNullable(), override));
to.setReadOnly(mergeProperty(to.getReadOnly(), from.getReadOnly(), override));
to.setWriteOnly(mergeProperty(to.getWriteOnly(), from.getWriteOnly(), override));
to.setExample(mergeProperty(to.getExample(), from.getExample(), override));
if (from.getExternalDocs() != null) {
if (to.getExternalDocs() == null) {
to.setExternalDocs(new ExternalDocumentationImpl());
}
ExternalDocumentationImpl.merge(from.getExternalDocs(), to.getExternalDocs(), override);
}
to.setDeprecated(mergeProperty(to.getDeprecated(), from.getDeprecated(), override));
if (from.getEnumeration() != null && from.getEnumeration().size() > 0) {
if (to.getEnumeration() == null) {
to.setEnumeration(createList());
}
for (Object value : from.getEnumeration()) {
if (!to.getEnumeration().contains(value)) {
to.addEnumeration(value);
}
}
}
if (from.getDiscriminator() != null) {
if (to.getDiscriminator() == null) {
to.setDiscriminator(new DiscriminatorImpl());
}
Discriminator discriminator = to.getDiscriminator();
discriminator.setPropertyName(mergeProperty(discriminator.getPropertyName(), from.getDiscriminator().getPropertyName(), override));
for (Entry<String, String> mapping : from.getDiscriminator().getMapping().entrySet()) {
discriminator.addMapping(mapping.getKey(), mapping.getValue());
}
}
if (from.getNot() != null) {
to.setNot(from.getNot());
}
mergeImmutableList(from.getAnyOf(), to.getAnyOf(), to::setAnyOf);
mergeImmutableList(from.getAllOf(), to.getAllOf(), to::setAllOf);
mergeImmutableList(from.getOneOf(), to.getOneOf(), to::setOneOf);
}
use of org.eclipse.microprofile.openapi.models.media.Discriminator in project Payara by payara.
the class ModelInvariantsTest method addKeyValueIgnoresNull.
@Test
public void addKeyValueIgnoresNull() {
BiPredicate<Extensible<?>, String> hasExtension = (obj, key) -> obj.getExtensions().containsKey(key);
assertAddIgnoresNull(new CallbackImpl(), Callback::addPathItem, Callback::hasPathItem);
assertAddIgnoresNull(new CallbackImpl(), Callback::addExtension, hasExtension);
assertAddIgnoresNull(new ExampleImpl(), Example::addExtension, hasExtension);
assertAddIgnoresNull(new HeaderImpl(), Header::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new HeaderImpl(), Header::addExtension, hasExtension);
assertAddIgnoresNull(new ContactImpl(), Contact::addExtension, hasExtension);
assertAddIgnoresNull(new InfoImpl(), Info::addExtension, hasExtension);
assertAddIgnoresNull(new LicenseImpl(), License::addExtension, hasExtension);
assertAddIgnoresNull(new LinkImpl(), Link::addParameter, (obj, key) -> obj.getParameters().containsKey(key));
assertAddIgnoresNull(new LinkImpl(), Link::addExtension, hasExtension);
assertAddIgnoresNull(new ContentImpl(), Content::addMediaType, Content::hasMediaType);
assertAddIgnoresNull(new DiscriminatorImpl(), Discriminator::addMapping, (obj, key) -> obj.getMapping().containsKey(key));
assertAddIgnoresNull(new EncodingImpl(), Encoding::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
assertAddIgnoresNull(new EncodingImpl(), Encoding::addExtension, hasExtension);
assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addEncoding, (obj, key) -> obj.getEncoding().containsKey(key));
assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addExtension, hasExtension);
assertAddIgnoresNull(new SchemaImpl(), Schema::addProperty, (obj, key) -> obj.getProperties().containsKey(key));
assertAddIgnoresNull(new SchemaImpl(), Schema::addExtension, hasExtension);
assertAddIgnoresNull(new XMLImpl(), XML::addExtension, hasExtension);
assertAddIgnoresNull(new ParameterImpl(), Parameter::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new ParameterImpl(), Parameter::addExtension, hasExtension);
assertAddIgnoresNull(new RequestBodyImpl(), RequestBody::addExtension, hasExtension);
assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addLink, (obj, key) -> obj.getLinks().containsKey(key));
assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addExtension, hasExtension);
assertAddIgnoresNull(new APIResponsesImpl(), APIResponses::addAPIResponse, APIResponses::hasAPIResponse);
assertAddIgnoresNull(new APIResponsesImpl(), APIResponses::addExtension, hasExtension);
assertAddIgnoresNull(new OAuthFlowImpl(), OAuthFlow::addExtension, hasExtension);
assertAddIgnoresNull(new OAuthFlowsImpl(), OAuthFlows::addExtension, hasExtension);
assertAddIgnoresNull(new SecuritySchemeImpl(), SecurityScheme::addExtension, hasExtension);
assertAddIgnoresNull(new ServerImpl(), Server::addExtension, hasExtension);
assertAddIgnoresNull(new ServerVariableImpl(), ServerVariable::addExtension, hasExtension);
assertAddIgnoresNull(new TagImpl(), Tag::addExtension, hasExtension);
assertAddIgnoresNull(new ComponentsImpl(), Components::addCallback, (obj, key) -> obj.getCallbacks().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addLink, (obj, key) -> obj.getLinks().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addParameter, (obj, key) -> obj.getParameters().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addRequestBody, (obj, key) -> obj.getRequestBodies().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addResponse, (obj, key) -> obj.getResponses().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addSchema, (obj, key) -> obj.getSchemas().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addSecurityScheme, (obj, key) -> obj.getSecuritySchemes().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addExtension, hasExtension);
assertAddIgnoresNull(new ExternalDocumentationImpl(), ExternalDocumentation::addExtension, hasExtension);
assertAddIgnoresNull(new OpenAPIImpl(), OpenAPI::addExtension, hasExtension);
assertAddIgnoresNull(new OperationImpl(), Operation::addCallback, (obj, key) -> obj.getCallbacks().containsKey(key));
assertAddIgnoresNull(new OperationImpl(), Operation::addExtension, hasExtension);
assertAddIgnoresNull(new PathItemImpl(), PathItem::addExtension, hasExtension);
assertAddIgnoresNull(new PathsImpl(), Paths::addPathItem, Paths::hasPathItem);
assertAddIgnoresNull(new PathsImpl(), Paths::addExtension, hasExtension);
}
use of org.eclipse.microprofile.openapi.models.media.Discriminator in project wildfly-swarm by wildfly-swarm.
the class OpenApiAnnotationScanner method readDiscriminatorMappings.
/**
* Reads an array of DiscriminatorMapping annotations into a {@link Discriminator} model.
* @param value
*/
private Discriminator readDiscriminatorMappings(AnnotationValue value) {
if (value == null) {
return null;
}
LOG.debug("Processing a list of @DiscriminatorMapping annotations.");
Discriminator discriminator = new DiscriminatorImpl();
AnnotationInstance[] nestedArray = value.asNestedArray();
for (@SuppressWarnings("unused") AnnotationInstance nested : nestedArray) {
// TODO iterate the discriminator mappings and do something sensible with them! :(
}
return discriminator;
}
Aggregations