Java Code Examples for org.eclipse.microprofile.openapi.models.OpenAPI#setTags()

The following examples show how to use org.eclipse.microprofile.openapi.models.OpenAPI#setTags() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelUtil.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a {@link Tag} to the {@link OpenAPI} model. If a tag having the same
 * name already exists in the model, the tags' attributes are merged, with the
 * new tag's attributes overriding the value of any attributes specified on
 * both.
 * 
 * @param openApi the OpenAPI model
 * @param tag a new {@link Tag} to add
 */
public static void addTag(OpenAPI openApi, Tag tag) {
    List<Tag> tags = openApi.getTags();

    if (tags == null || tags.isEmpty()) {
        openApi.addTag(tag);
        return;
    }

    Tag current = tags.stream().filter(t -> t.getName().equals(tag.getName())).findFirst().orElse(null);
    int currentIndex = tags.indexOf(current);

    if (current != null) {
        Tag replacement = MergeUtil.mergeObjects(current, tag);
        tags = new ArrayList<>(tags);
        tags.set(currentIndex, replacement);
        openApi.setTags(tags);
    } else {
        openApi.addTag(tag);
    }
}
 
Example 2
Source File: DefinitionReader.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a OpenAPIDefinition annotation.
 * 
 * @param context the scanning context
 * @param openApi OpenAPIImpl
 * @param annotationInstance {@literal @}OpenAPIDefinition annotation
 */
public static void processDefinition(final AnnotationScannerContext context,
        final OpenAPI openApi,
        final AnnotationInstance annotationInstance) {
    IoLogging.log.annotation("@OpenAPIDefinition");

    openApi.setInfo(InfoReader.readInfo(annotationInstance.value(DefinitionConstant.PROP_INFO)));
    openApi.setTags(TagReader.readTags(annotationInstance.value(DefinitionConstant.PROP_TAGS)).orElse(null));
    openApi.setServers(
            ServerReader.readServers(annotationInstance.value(DefinitionConstant.PROP_SERVERS)).orElse(null));
    openApi.setSecurity(SecurityRequirementReader
            .readSecurityRequirements(annotationInstance.value(DefinitionConstant.PROP_SECURITY)).orElse(null));
    openApi.setExternalDocs(
            ExternalDocsReader
                    .readExternalDocs(annotationInstance.value(ExternalDocsConstant.PROP_EXTERNAL_DOCS)));
    openApi.setComponents(ComponentsReader.readComponents(context,
            annotationInstance.value(DefinitionConstant.PROP_COMPONENTS)));
}
 
Example 3
Source File: DefinitionReader.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a OpenAPIDefinition Json node.
 * 
 * @param openApi the OpenAPI model
 * @param node the Json node
 */
public static void processDefinition(final OpenAPI openApi,
        final JsonNode node) {
    IoLogging.log.jsonNode("OpenAPIDefinition");

    openApi.setOpenapi(JsonUtil.stringProperty(node, DefinitionConstant.PROP_OPENAPI));
    openApi.setInfo(InfoReader.readInfo(node.get(DefinitionConstant.PROP_INFO)));
    openApi.setTags(TagReader.readTags(node.get(DefinitionConstant.PROP_TAGS)).orElse(null));
    openApi.setServers(ServerReader.readServers(node.get(DefinitionConstant.PROP_SERVERS)).orElse(null));
    openApi.setSecurity(SecurityRequirementReader
            .readSecurityRequirements(node.get(DefinitionConstant.PROP_SECURITY)).orElse(null));
    openApi.setExternalDocs(
            ExternalDocsReader.readExternalDocs(node.get(ExternalDocsConstant.PROP_EXTERNAL_DOCS)));
    openApi.setComponents(
            ComponentsReader.readComponents(node.get(DefinitionConstant.PROP_COMPONENTS)));
    openApi.setPaths(PathsReader.readPaths(node.get(DefinitionConstant.PROP_PATHS)));
    ExtensionReader.readExtensions(node, openApi);
}
 
Example 4
Source File: AnnotationScanner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Sort the tags unless the application has defined the order in OpenAPIDefinition annotation(s)
 * 
 * @param openApi the openAPI model
 * @param tagsDefined is the tags defined
 */
default void sortTags(OpenAPI openApi, boolean tagsDefined) {

    if (!tagsDefined && openApi.getTags() != null) {
        openApi.setTags(openApi.getTags()
                .stream()
                .sorted(Comparator.comparing(Tag::getName))
                .collect(Collectors.toList()));
    }
}
 
Example 5
Source File: ModelConstructionTest.java    From microprofile-open-api with Apache License 2.0 4 votes vote down vote up
@Test
public void openAPITest() {
    final OpenAPI o = processConstructible(OpenAPI.class);
    
    final SecurityRequirement sr = createConstructibleInstance(SecurityRequirement.class);
    sr.addScheme("BasicAuth");
    checkSameObject(o, o.addSecurityRequirement(sr));
    checkListEntry(o.getSecurity(), sr);
    assertEquals(o.getSecurity().size(), 1, "The list is expected to contain one entry.");
    o.removeSecurityRequirement(sr);
    assertEquals(o.getSecurity().size(), 0, "The list is expected to be empty.");
    
    final SecurityRequirement sr2 = createConstructibleInstance(SecurityRequirement.class);
    sr2.addScheme("OAuth2", "read");
    o.setSecurity(Collections.singletonList(sr2));
    assertEquals(o.getSecurity().size(), 1, "The list is expected to contain one entry.");
    checkListEntry(o.getSecurity(), sr2);
    checkSameObject(o, o.addSecurityRequirement(sr));
    assertEquals(o.getSecurity().size(), 2, "The list is expected to contain two entries.");
    checkListEntry(o.getSecurity(), sr);
    
    SecurityRequirement otherSecurityRequirementValue  = createConstructibleInstance(SecurityRequirement.class);
    otherSecurityRequirementValue.addScheme("OAuth2", "admin");
    checkListImmutable(o, OpenAPI::getSecurity, otherSecurityRequirementValue);
    
    final Server s = createConstructibleInstance(Server.class);
    checkSameObject(o, o.addServer(s));
    checkListEntry(o.getServers(), s);
    assertEquals(o.getServers().size(), 1, "The list is expected to contain one entry.");
    o.removeServer(s);
    assertEquals(o.getServers().size(), 0, "The list is expected to be empty.");
    
    final Server s2 = createConstructibleInstance(Server.class);
    o.setServers(Collections.singletonList(s2));
    assertEquals(o.getServers().size(), 1, "The list is expected to contain one entry.");
    checkListEntry(o.getServers(), s2);
    checkSameObject(o, o.addServer(s));
    assertEquals(o.getSecurity().size(), 2, "The list is expected to contain two entries.");
    checkListEntry(o.getServers(), s);
    
    Server otherServer  = createConstructibleInstance(Server.class);
    checkListImmutable(o, OpenAPI::getServers, otherServer);
    
    final Tag t = createConstructibleInstance(Tag.class);
    checkSameObject(o, o.addTag(t));
    checkListEntry(o.getTags(), t);
    assertEquals(o.getTags().size(), 1, "The list is expected to contain one entry.");
    o.removeTag(t);
    assertEquals(o.getTags().size(), 0, "The list is expected to be empty.");
    
    final Tag t2 = createConstructibleInstance(Tag.class);
    o.setTags(Collections.singletonList(t2));
    assertEquals(o.getTags().size(), 1, "The list is expected to contain one entry.");
    checkListEntry(o.getTags(), t2);
    checkSameObject(o, o.addTag(t));
    assertEquals(o.getSecurity().size(), 2, "The list is expected to contain two entries.");
    checkListEntry(o.getTags(), t);
    
    Tag otherTag  = createConstructibleInstance(Tag.class);
    checkListImmutable(o, OpenAPI::getTags, otherTag);
}