Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller returns links and not _links and much more (see body). #1572

Closed
talentedasian opened this issue Jul 6, 2021 · 6 comments
Closed
Assignees
Labels
in: mediatypes Media type related functionality

Comments

@talentedasian
Copy link

Whenever I test my controller response with MockMvc, it does not return the correct response for links.

  1. See the title of this issue.
  2. Links are rendered as an array instead of key value pairs. For example it should be
_links: {

    }

and not

links:[  {  }  ]

The test is a slice test only testing a single controller. The test class is annotated with @WebMvcTest(controller.class) and @AutoConfigureMockMvc.

@odrotbohm
Copy link
Member

This suspiciously looks like you're not causing the HAL rendering to be active. It's hard to tell what causes this without further information because it could be the server set up, the test setup, the setup of the request being executed etc. Can you provide a minimal example project that reproduces the error?

@odrotbohm odrotbohm self-assigned this Jul 6, 2021
@odrotbohm odrotbohm added in: mediatypes Media type related functionality process: waiting for feedback labels Jul 6, 2021
@talentedasian
Copy link
Author

@WebMvcTest({Oauth2.class})
public class Oauth2ControllerTest {

	public MockMvc mvc;
	
	@BeforeEach
	public void setup(WebApplicationContext wac) {
		mvc = MockMvcBuilders.webAppContextSetup(wac)
				.alwaysDo(MockMvcResultHandlers.print())
				.build();
	}
	
	@Test
	public void shouldReturnHalForms() throws Exception {
		String jwt = JwtProvider.createJwtWithFixedExpirationDate("test sub", "1111", "test name");
		Cookie cookie = new Cookie("accessJwt", jwt);
		Cookie[] cookies = {cookie};
		
		mvc.perform(get(create("/login/oauth2/code/facebook"))
				.accept(MediaTypes.HAL_FORMS_JSON)
				.cookie(cookies))
			.andExpect(status().isOk());
	}

This test prints the links as arrays and not as key value pairs but doing a full spring boot test with the @SpringBootTest fixes this. I only need to test one controller so I am forced to do this up until I get a fix.

@odrotbohm
Copy link
Member

I'd really have to get an executable sample to be able to debug it to tell more.

@gregturn
Copy link
Contributor

gregturn commented Jul 6, 2021

Your web controller methods need to be returning Spring HATEOAS types (RepresentationModel or one of its subtypes) for things to render properly.

@talentedasian
Copy link
Author

talentedasian commented Jul 6, 2021

Your web controller methods need to be returning Spring HATEOAS types (RepresentationModel or one of its subtypes) for things to render properly.

My DTO(response rather) extends RepresentationModel

@odrotbohm
Copy link
Member

Closing this due to the lack of feedback required to further diagnose this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mediatypes Media type related functionality
Projects
None yet
Development

No branches or pull requests

3 participants