Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public class SpringDocDataRestUtils {
*/
private final HashMap<String, EntityInfo> entityInoMap = new HashMap();

/**
* The associations fields by entity.
*/
private final HashMap<String, List<String>> allAssociationsFieldsMap = new HashMap<>();

/**
* The Repository rest configuration.
*/
Expand Down Expand Up @@ -136,6 +141,7 @@ public void customise(OpenAPI openAPI, ResourceMappings mappings, PersistentEnti
entityInfo.setIgnoredFields(ignoredFields);
List<String> associationsFields = getAssociationsFields(resourceMetadata, entity);
entityInfo.setAssociationsFields(associationsFields);
allAssociationsFieldsMap.put(domainType.getSimpleName(), getAllAssociationsFields(resourceMetadata, entity));
entityInoMap.put(domainType.getSimpleName(), entityInfo);
}

Expand Down Expand Up @@ -277,11 +283,43 @@ private Schema updateResponseSchema(String className, Schema existingSchema, Com
else if (EMBEDDED.equals(propId)) {
updateResponseSchemaEmbedded(components, entityInfo, entry, openapi31);
}
else if (allAssociationsFieldsMap.get(className).contains(propId)) {
updateResponseSchemaProperty(entry.getValue(), components, openapi31);
}
}
}
return existingSchema;
}

/**
* Update a response schema property that points to an entity which is not an
* exported repository. Spring Data REST serializes these associations in
* the containing representation, so they need the same association filtering
* as an exported entity response.
*
* @param property the property
* @param components the components
* @param openapi31 the openapi 31
*/
private void updateResponseSchemaProperty(Schema property, Components components, boolean openapi31) {
if (property == null)
return;
if (property.get$ref() != null && !property.get$ref().endsWith(RESPONSE)) {
String key = property.get$ref().substring(Components.COMPONENTS_SCHEMAS_REF.length());
if (entityInoMap.containsKey(key)) {
String newKey = property.get$ref() + RESPONSE;
if (!components.getSchemas().containsKey(key + RESPONSE)) {
createNewResponseSchema(key, components, openapi31);
updateResponseSchema(key, components.getSchemas().get(key + RESPONSE), components, openapi31);
}
property.set$ref(newKey);
}
}
else if (property.getItems() != null) {
updateResponseSchemaProperty(property.getItems(), components, openapi31);
}
}

/**
* Update response schema embedded.
*
Expand Down Expand Up @@ -447,6 +485,25 @@ private List<String> getAssociationsFields(ResourceMetadata
return associationsFields;
}

/**
* Gets all associations fields.
*
* @param resourceMetadata the resource metadata
* @param entity the entity
* @return all associations fields
*/
private List<String> getAllAssociationsFields(ResourceMetadata
resourceMetadata, PersistentEntity<?, ?> entity) {
List<String> associationsFields = new ArrayList<>();
entity.doWithAssociations((SimpleAssociationHandler) association -> {
PersistentProperty<?> property = association.getInverse();
ResourceMapping mapping = resourceMetadata.getMappingFor(property);
String fieldName = mapping.getRel().value();
associationsFields.add(fieldName);
});
return associationsFields;
}

/**
* Gets ignored fields.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
*
* * Copyright 2019-2026 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.v31.app40;

import java.util.HashSet;
import java.util.Set;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;

/**
* Aggregate root with a collection of composite-key join entities.
*
* @author hej090224
*/
@Entity
public class Goal {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

@OneToMany(mappedBy = "goal", cascade = CascadeType.ALL)
private Set<InitiativeImpactOnGoal> impactsByInitiatives = new HashSet<>();

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Set<InitiativeImpactOnGoal> getImpactsByInitiatives() {
return impactsByInitiatives;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
*
* * Copyright 2019-2026 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.v31.app40;

import java.io.Serializable;

import jakarta.persistence.Embeddable;

/**
* Composite identifier for the join entity used by the issue reproducer.
*
* @author hej090224
*/
@Embeddable
public class GoalInitiativeImpactId implements Serializable {

private Long goalId;

private Long initiativeId;

public Long getGoalId() {
return goalId;
}

public void setGoalId(Long goalId) {
this.goalId = goalId;
}

public Long getInitiativeId() {
return initiativeId;
}

public void setInitiativeId(Long initiativeId) {
this.initiativeId = initiativeId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* * Copyright 2019-2026 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.v31.app40;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

/**
* Spring Data REST repository for the issue reproducer aggregate.
*
* @author hej090224
*/
@RepositoryRestResource
public interface GoalRepository extends CrudRepository<Goal, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* * Copyright 2019-2026 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.v31.app40;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

/**
* Entity referenced by the composite-key join entity.
*
* @author hej090224
*/
@Entity
public class Initiative {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

public Long getId() {
return id;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* * Copyright 2019-2026 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.v31.app40;

import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;

/**
* Join entity combining an embedded identifier with two mapped associations.
*
* @author hej090224
*/
@Entity
public class InitiativeImpactOnGoal {

@EmbeddedId
private GoalInitiativeImpactId id;

@ManyToOne
@MapsId("goalId")
@JoinColumn(name = "goal_id")
private Goal goal;

@ManyToOne
@MapsId("initiativeId")
@JoinColumn(name = "initiative_id")
private Initiative initiative;

@Enumerated(EnumType.STRING)
private ImpactLevel impactLevel;

public GoalInitiativeImpactId getId() {
return id;
}

public Goal getGoal() {
return goal;
}

public Initiative getInitiative() {
return initiative;
}

public ImpactLevel getImpactLevel() {
return impactLevel;
}

enum ImpactLevel {
TRIVIAL,
SIGNIFICANT
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* * Copyright 2019-2026 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.v31.app40;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

/**
* Spring Data REST repository for initiatives.
*
* @author hej090224
*/
@RepositoryRestResource
public interface InitiativeRepository extends CrudRepository<Initiative, Long> {
}
Loading