Skip to content

Commit

Permalink
[#10] Add setterTarget to referenced entries, add ReferencedItemBuild…
Browse files Browse the repository at this point in the history
…Context
  • Loading branch information
neiser committed Jun 28, 2021
1 parent 123a385 commit 2c13f4a
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@
import de.qaware.openapigeneratorforspring.common.reference.fortype.ReferenceDeciderForType;
import de.qaware.openapigeneratorforspring.common.reference.fortype.ReferenceIdentifierBuilderForType;
import de.qaware.openapigeneratorforspring.common.reference.fortype.ReferenceIdentifierConflictResolverForType;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.common.util.OpenApiProxyUtils;
import de.qaware.openapigeneratorforspring.model.trait.HasReference;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -63,6 +66,10 @@ public abstract class AbstractReferencedItemStorage<T extends HasReference<T>> {

private final List<Entry<T>> entries = new ArrayList<>();

public static ReferencedItemBuildContext createContext() {
return null;
}

@Builder
@Getter(AccessLevel.PRIVATE)
protected static class AddEntryParameters {
Expand Down Expand Up @@ -93,7 +100,23 @@ protected void addEntry(T item, Consumer<T> setter, AddEntryParameters options)
ReferenceSetter.of(setter, options.isReferenceRequired()),
options.getSuggestedIdentifier()
);
entries.add(Entry.of(item, referenceSetterWithIdentifier, options.getOwner()));
entries.add(Entry.of(item, referenceSetterWithIdentifier, options.getOwner(), findSetterTarget(setter)));
}

@Nullable
private Object findSetterTarget(Consumer<T> setter) {
Field[] declaredFields = setter.getClass().getDeclaredFields();
if (declaredFields.length == 1) {
Field declaredField = declaredFields[0];
declaredField.setAccessible(true);
try {
return declaredField.get(setter);
} catch (IllegalAccessException e) {
// this should not happen, as we've made it accessible...
return null;
}
}
return null;
}

protected void removeEntriesOwnedBy(Object owner) {
Expand All @@ -104,7 +127,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
return Pair.of(referenceType, buildDependencies);
}

public void buildReferencedItems(Consumer<Map<String, T>> referencedItemsConsumer) {
public void buildReferencedItems(Consumer<Map<String, T>> referencedItemsConsumer, @Nullable ReferencedItemBuildContext context) {
Map<String, T> referencedItems = new LinkedHashMap<>();
ReferenceActions referenceActions = new ReferenceActions();
buildGroupedEntries()
Expand Down Expand Up @@ -257,21 +280,15 @@ private GroupedEntry createNewGroupedEntry(Entry<T> entry) {

@RequiredArgsConstructor(staticName = "of")
@Getter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
private static class Entry<T> {
@EqualsAndHashCode.Include
private final T item;
private final ReferenceSetterWithIdentifier<T> referenceSetterWithIdentifier;
@Nullable
private final Object owner;

@Override
public boolean equals(Object obj) {
return obj instanceof Entry<?> && item.equals(((Entry<?>) obj).item);
}

@Override
public int hashCode() {
return item.hashCode();
}
@Nullable
private final Object setterTarget;
}

@RequiredArgsConstructor(staticName = "of")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package de.qaware.openapigeneratorforspring.common.reference;

import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemHandlerFactory;
import de.qaware.openapigeneratorforspring.model.Components;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void applyToOpenApi(OpenApi openApi) {
return (AbstractDependentReferencedComponentHandler) itemHandler;
}
// non-dependent handler can already be handled here
itemHandler.applyToOpenApi(openApi);
itemHandler.applyToOpenApi(openApi, null);
return null;
})
.filter(Objects::nonNull)
Expand Down Expand Up @@ -103,11 +104,13 @@ public DependentReferencedComponentHandlersSupport(OpenApi openApi, List<Abstrac
public void handle() {
Map<ReferenceType, Set<ReferenceType>> transitiveDependencies = handlersMap.keySet().stream()
.collect(Collectors.toMap(r -> r, r -> buildTransitiveDependencies(r).collect(Collectors.toSet())));

ReferencedItemBuildContext buildContext = AbstractReferencedItemStorage.createContext();
handlersMap.values().stream()
.sorted(buildDependencyComparator(transitiveDependencies)) // makes less-dependent handlers go first!
.forEach(handler -> {
LOGGER.debug("Building components for {}", handler.getType());
handler.getHandler().applyToOpenApi(openApi);
handler.getHandler().applyToOpenApi(openApi, buildContext);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.operation.Callback;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -48,7 +50,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setCallbacks);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setCallbacks, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.example.Example;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -48,8 +50,8 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setExamples);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setExamples, context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.header.Header;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -48,7 +50,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setHeaders);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setHeaders, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.link.Link;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -48,7 +50,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setLinks);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setLinks, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.parameter.Parameter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -52,7 +53,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setParameters);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setParameters, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.requestbody.RequestBody;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Consumer;

Expand All @@ -47,7 +49,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setRequestBodies);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setRequestBodies, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.response.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Consumer;

Expand All @@ -47,7 +49,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setResponses);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setResponses, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import de.qaware.openapigeneratorforspring.common.reference.ReferenceType;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractDependentReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.media.Schema;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Consumer;

Expand Down Expand Up @@ -54,7 +56,7 @@ public Pair<ReferenceType, List<ReferenceType>> getBuildDependencies() {
}

@Override
public void applyToComponents(Components components) {
storage.buildReferencedItems(components::setSchemas);
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
storage.buildReferencedItems(components::setSchemas, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

import de.qaware.openapigeneratorforspring.common.mapper.SecuritySchemeAnnotationMapper;
import de.qaware.openapigeneratorforspring.common.reference.handler.AbstractReferencedComponentHandler;
import de.qaware.openapigeneratorforspring.common.reference.handler.ReferencedItemBuildContext;
import de.qaware.openapigeneratorforspring.common.security.OpenApiSecuritySchemesSupplier;
import de.qaware.openapigeneratorforspring.common.supplier.OpenApiSpringBootApplicationAnnotationsSupplier;
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -80,7 +82,7 @@ public void accept(Map<String, SecurityScheme> map) {
}

@Override
public void applyToComponents(Components components) {
public void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context) {
setMapIfNotEmpty(securitySchemes, components::setSecuritySchemes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.util.List;

// TODO do not derive from AbstractReferencedComponentHandler
public abstract class AbstractDependentReferencedComponentHandler extends AbstractReferencedComponentHandler {

public abstract Pair<ReferenceType, List<ReferenceType>> getBuildDependencies();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
import de.qaware.openapigeneratorforspring.model.Components;
import de.qaware.openapigeneratorforspring.model.OpenApi;

import javax.annotation.Nullable;
import java.util.Objects;

public abstract class AbstractReferencedComponentHandler implements ReferencedItemHandler {

@Override
public final void applyToOpenApi(OpenApi openApi) {
public final void applyToOpenApi(OpenApi openApi, @Nullable ReferencedItemBuildContext context) {
// ReferencedItemSupportFactory ensures that openApi.getComponents() is not null
applyToComponents(Objects.requireNonNull(openApi.getComponents(), "components of openApi must not be null"));
applyToComponents(
Objects.requireNonNull(openApi.getComponents(), "components of openApi must not be null"),
context
);
}

protected abstract void applyToComponents(Components components);
protected abstract void applyToComponents(Components components, @Nullable ReferencedItemBuildContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*-
* #%L
* OpenAPI Generator for Spring Boot :: Common
* %%
* Copyright (C) 2020 QAware GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

package de.qaware.openapigeneratorforspring.common.reference.handler;

public interface ReferencedItemBuildContext {
}
Loading

0 comments on commit 2c13f4a

Please sign in to comment.