Skip to content

Commit

Permalink
Add support for builtin_section.
Browse files Browse the repository at this point in the history
Resolves #38.

Also use pattern variables in a few places
  • Loading branch information
rmgrimm committed Oct 25, 2023
1 parent 354be6c commit 3e4e84e
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ public Optional<InputStream> getTemplatePublished(long templateId) throws ApiExc

@Override
public void save(@Nonnull CmsSection section) throws ApiException {
if (section instanceof CmsBuiltinSection) {
saveBuiltinSection((CmsBuiltinSection) section);
} else {
saveSection(section);
}
}

private void saveBuiltinSection(CmsBuiltinSection builtinSection) {
throw new UnsupportedOperationException("CmsBuiltinSection cannot be modified");
}

private void saveSection(CmsSection section) throws ApiException {
Section restSection = SECTION_MAPPER.toRest(section);
if (section.getId() == null) {
if (StringUtils.isBlank(restSection.getTitle())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fwmotion.threescale.cms.mappers;

import com.fwmotion.threescale.cms.model.CmsBuiltinSection;
import com.redhat.threescale.rest.cms.model.BuiltinSection;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper
public interface CmsBuiltinSectionMapper {

@Mapping(target = "path", source = "partialPath")
CmsBuiltinSection fromRest(BuiltinSection builtinSection);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fwmotion.threescale.cms.mixins;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.redhat.threescale.rest.cms.model.BuiltinSection;
import com.redhat.threescale.rest.cms.model.Section;
import jakarta.xml.bind.annotation.XmlElement;

Expand All @@ -10,10 +12,22 @@
/**
* @see com.redhat.threescale.rest.cms.model.SectionList
*/
@JsonDeserialize(as = SectionMergingList.class)
public interface SectionListMixIn {

String ELEMENT_NAME_BUILTIN_SECTION = "builtin_section";
String ELEMENT_NAME_SECTION = "section";

@JsonProperty(ELEMENT_NAME_SECTION)
@XmlElement(name = ELEMENT_NAME_SECTION)
@JacksonXmlElementWrapper(useWrapping = false, localName = ELEMENT_NAME_BUILTIN_SECTION)
List<BuiltinSection> getBuiltinSections();

@JsonProperty(ELEMENT_NAME_BUILTIN_SECTION)
@XmlElement(name = ELEMENT_NAME_BUILTIN_SECTION)
@JacksonXmlElementWrapper(useWrapping = false, localName = ELEMENT_NAME_BUILTIN_SECTION)
void setBuiltinSections(List<BuiltinSection> sections);

@JsonProperty(ELEMENT_NAME_SECTION)
@XmlElement(name = ELEMENT_NAME_SECTION)
@JacksonXmlElementWrapper(useWrapping = false, localName = ELEMENT_NAME_SECTION)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fwmotion.threescale.cms.mixins;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.redhat.threescale.rest.cms.model.BuiltinSection;
import com.redhat.threescale.rest.cms.model.Section;
import com.redhat.threescale.rest.cms.model.SectionList;
import jakarta.xml.bind.annotation.XmlElement;

import java.util.List;

/**
* This is used for deserialization to facilitate interlaced unwrapped lists.
* Per
* <a href="https://github.com/FasterXML/jackson-dataformat-xml/issues/275">jackson-dataformat-xml Issue #275</a>,
* interlaced unwrapped lists is not supported, so this class will be used
* as a workaround to merge the lists as they're "set" into the object.
*
* @see SectionListMixIn
* @see SectionList
*/
public class SectionMergingList extends SectionList {

@JsonProperty(SectionListMixIn.ELEMENT_NAME_BUILTIN_SECTION)
@XmlElement(name = SectionListMixIn.ELEMENT_NAME_BUILTIN_SECTION)
@JacksonXmlElementWrapper(useWrapping = false, localName = SectionListMixIn.ELEMENT_NAME_BUILTIN_SECTION)
@Override
public void setBuiltinSections(List<BuiltinSection> builtinSections) {
builtinSections.forEach(super::addBuiltinSectionsItem);
}

@JsonProperty(SectionListMixIn.ELEMENT_NAME_BUILTIN_SECTION)
@XmlElement(name = SectionListMixIn.ELEMENT_NAME_BUILTIN_SECTION)
@JacksonXmlElementWrapper(useWrapping = false, localName = SectionListMixIn.ELEMENT_NAME_BUILTIN_SECTION)
@Override
public void setSections(List<Section> sections) {
sections.forEach(super::addSectionsItem);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.fwmotion.threescale.cms.model;

/**
* Subclass of {@link CmsSection} to differentiate builtin_section from section.
* <p>
* The properties all appear to remain identical.
*/
public class CmsBuiltinSection extends CmsSection {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.time.OffsetDateTime;

public class CmsSection implements CmsObject{
public class CmsSection implements CmsObject {

private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;
Expand Down Expand Up @@ -93,9 +93,7 @@ public void setPath(String path) {
public boolean equals(Object o) {
if (this == o) return true;

if (!(o instanceof CmsSection)) return false;

CmsSection that = (CmsSection) o;
if (!(o instanceof CmsSection that)) return false;

return new EqualsBuilder().append(getId(), that.getId()).isEquals();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fwmotion.threescale.cms.support;

import com.fwmotion.threescale.cms.mappers.CmsBuiltinSectionMapper;
import com.fwmotion.threescale.cms.mappers.CmsSectionMapper;
import com.fwmotion.threescale.cms.model.CmsSection;
import com.redhat.threescale.rest.cms.ApiException;
Expand All @@ -14,10 +15,12 @@

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PagedSectionsSpliterator extends AbstractPagedRestApiSpliterator<CmsSection> {

private static final CmsSectionMapper SECTION_MAPPER = Mappers.getMapper(CmsSectionMapper.class);
private static final CmsBuiltinSectionMapper BUILTIN_SECTION_MAPPER = Mappers.getMapper(CmsBuiltinSectionMapper.class);

private final SectionsApi sectionsApi;

Expand Down Expand Up @@ -47,10 +50,15 @@ protected Collection<CmsSection> getPage(@PositiveOrZero int pageNumber,
try {
SectionList sectionList = sectionsApi.listSections(pageNumber, pageSize);

List<CmsSection> resultPage = ListUtils.emptyIfNull(sectionList.getSections())
.stream()
.map(SECTION_MAPPER::fromRest)
.collect(Collectors.toList());
List<CmsSection> resultPage =
Stream.concat(
ListUtils.emptyIfNull(sectionList.getBuiltinSections())
.stream()
.map(BUILTIN_SECTION_MAPPER::fromRest),
ListUtils.emptyIfNull(sectionList.getSections())
.stream()
.map(SECTION_MAPPER::fromRest)
).collect(Collectors.toList());

validateResultPageSize(
"section",
Expand Down
53 changes: 40 additions & 13 deletions rest-client/src/main/resources/api-spec/3scale-cms.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.3
info:
title: 3scale CMS API
version: '2.12'
version: '2.14'
description: 3scale's undocumented content management system API
servers:
- url: 'https://{tenant-name}-admin.{ocp-wildcard-url}'
Expand All @@ -22,7 +22,7 @@ paths:
- name: page
description: |
The number for the page of results to retrieve, starting from page 1;
defautls to 1
defaults to 1
schema:
type: integer
minimum: 1
Expand Down Expand Up @@ -170,7 +170,7 @@ paths:
- Sections
responses:
'200':
$ref: '#/components/responses/SectionResponse'
$ref: '#/components/responses/SectionTypeResponse'
operationId: get-section
summary: Get Section
description: Get section descriptor
Expand Down Expand Up @@ -216,7 +216,7 @@ paths:
- Sections
responses:
'200':
$ref: '#/components/responses/SectionResponse'
$ref: '#/components/responses/SectionTypeResponse'
operationId: get-section-by-system-name
summary: Get Section by System Name
description: Get section descriptor
Expand Down Expand Up @@ -996,6 +996,23 @@ components:
type: integer
format: int64
example: 3
BuiltinSection:
description: Builtin section within 3scale CMS
allOf:
- $ref: '#/components/schemas/Section'
xml:
name: builtin_section
example: |-
<builtin_section>
<id>1974454</id>
<created_at>2016-10-29T17:55:09-05:00</created_at>
<updated_at>2020-07-22T16:08:40-05:00</updated_at>
<title>Root</title>
<system_name>root</system_name>
<public>true</public>
<parent_id/>
<partial_path>/</partial_path>
</builtin_section>
Section:
description: Section within 3scale CMS
allOf:
Expand Down Expand Up @@ -1047,6 +1064,13 @@ components:
allOf:
- $ref: '#/components/schemas/ListPaginationAttributes'
- properties:
builtinSections:
type: array
items:
$ref: '#/components/schemas/BuiltinSection'
xml:
name: builtin_section
wrapped: false
sections:
type: array
items:
Expand All @@ -1059,7 +1083,7 @@ components:
example: |-
<?xml version="1.0" encoding="UTF-8"?>
<sections per_page="20" total_entries="25" total_pages="2" current_page="1">
<section>
<builtin_section>
<id>30</id>
<created_at>2022-03-05T04:48:27Z</created_at>
<updated_at>2022-03-18T06:31:57Z</updated_at>
Expand Down Expand Up @@ -1260,6 +1284,10 @@ components:
<system_name>search</system_name>
</section>
</sections>
SectionType:
oneOf:
- $ref: '#/components/schemas/BuiltinSection'
- $ref: '#/components/schemas/Section'
SectionUpdatableFields:
description: Fields that may be modified after section creation
properties:
Expand Down Expand Up @@ -1629,14 +1657,7 @@ components:
maxLength: 255
responses:
EmptyResponse:
content:
application/xml:
schema:
default: ''
maxLength: 0
minLength: 0
type: string
description: Response indicating successful deletion
description: Response with no body; eg, to indicate successful deletion
ErrorHashResponse:
content:
application/xml:
Expand Down Expand Up @@ -1691,6 +1712,12 @@ components:
schema:
$ref: '#/components/schemas/Section'
description: Response containing a single `Section`
SectionTypeResponse:
content:
application/xml:
schema:
$ref: '#/components/schemas/SectionType'
description: Response containing a single `SectionType`
securitySchemes:
provider_key:
type: apiKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void listAllCmsObjects() {
assertThat(result, hasSize(97));
assertThat(result, hasItems(
FilesApiTestSupport.FAVICON_FILE_MATCHER,
SectionsApiTestSupport.ROOT_SECTION_MATCHER,
SectionsApiTestSupport.ROOT_BUILTIN_SECTION_MATCHER,
SectionsApiTestSupport.CSS_SECTION_MATCHER,
TemplatesApiTestSupport.MAIN_LAYOUT_MATCHER
));
}
Expand All @@ -80,4 +81,5 @@ void getFileContent() throws Exception {

assertThat(result.get(), inputStreamContents(equalTo("This is a test value.\n")));
}

}
Loading

0 comments on commit 3e4e84e

Please sign in to comment.