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

Add content unit option to customize referencable entities #130

Merged
merged 2 commits into from
Sep 9, 2024

Conversation

vearutop
Copy link
Member

@vearutop vearutop commented Sep 9, 2024

This allows setting up request/response references with ease, also it allows for direct access to respective entities for fine-tuning (setting uncommon properties).

	r := openapi31.NewReflector()

	op, err := r.NewOperationContext(http.MethodPost, "/{document_id}/{client}")
	require.NoError(t, err)

	op.AddReqStructure(new(struct {
		DocumentID string `path:"document_id"`
		Client     string `path:"client"`
		Foo        int    `json:"foo"`
	}), openapi.WithCustomize(func(cor openapi.ContentOrReference) {
		_, ok := cor.(*openapi31.RequestBodyOrReference)
		assert.True(t, ok)

		cor.SetReference("../somewhere/components/requests/foo.yaml")
	}))

	op.AddRespStructure(
		nil, openapi.WithReference("../somewhere/components/responses/204.yaml"), openapi.WithHTTPStatus(204),
	)
	op.AddRespStructure(
		nil, openapi.WithCustomize(func(cor openapi.ContentOrReference) {
			_, ok := cor.(*openapi31.ResponseOrReference)
			assert.True(t, ok)

			cor.SetReference("../somewhere/components/responses/200.yaml")
		}), openapi.WithHTTPStatus(200),
	)

	require.NoError(t, r.AddOperation(op))

	assertjson.EqMarshal(t, `{
	  "openapi":"3.1.0","info":{"title":"","version":""},
	  "paths":{
		"/{document_id}/{client}":{
		  "post":{
			"parameters":[
			  {
				"name":"document_id","in":"path","required":true,
				"schema":{"type":"string"}
			  },
			  {
				"name":"client","in":"path","required":true,
				"schema":{"type":"string"}
			  }
			],
			"requestBody":{"$ref":"../somewhere/components/requests/foo.yaml"},
			"responses":{
			  "200":{"$ref":"../somewhere/components/responses/200.yaml"},
			  "204":{"$ref":"../somewhere/components/responses/204.yaml"}
			}
		  }
		}
	  }
	}`, r.SpecSchema())

Copy link

github-actions bot commented Sep 9, 2024

Lines Of Code

Language Files Lines Code Comments Blanks Complexity Bytes
Go 21 4032 (+74) 2991 (+46) 227 (+13) 814 (+15) 819 (+12) 92.7K (+1.7K)
Go (test) 22 6545 (+114) 5020 (+98) 660 865 (+16) 61 168.5K (+3.1K)

Copy link

github-actions bot commented Sep 9, 2024

Go API Changes

# github.com/swaggest/openapi-go
## compatible changes
ContentOrReference: added
ContentUnit.Customize: added
WithCustomize: added
WithReference: added

# github.com/swaggest/openapi-go/openapi3
## compatible changes
(*RequestBodyOrRef).SetReference: added
(*ResponseOrRef).SetReference: added

# github.com/swaggest/openapi-go/openapi31
## compatible changes
(*RequestBodyOrReference).SetReference: added
(*ResponseOrReference).SetReference: added

# summary
Inferred base version: v0.2.53
Suggested version: v0.3.0

Copy link

github-actions bot commented Sep 9, 2024

Unit Test Coverage

total: (statements) 47.4%
changed lines: (statements) 81.5%, coverage is less than 90.0%, consider testing the changes more thoroughly

Coverage of changed lines
File Function Coverage
Total 81.5%
openapi3/helper.go 100.0%
openapi3/helper.go:215 SetReference 100.0%
openapi3/helper.go:221 SetReference 100.0%
openapi3/reflect.go 100.0%
openapi3/reflect.go:271 setupRequest 100.0%
openapi3/reflect.go:620 setupResponse 100.0%
openapi31/helper.go 100.0%
openapi31/helper.go:274 SetReference 100.0%
openapi31/helper.go:280 SetReference 100.0%
openapi31/reflect.go 100.0%
openapi31/reflect.go:218 setupRequest 100.0%
openapi31/reflect.go:571 setupResponse 100.0%
operation.go 0.0%
operation.go:59 WithCustomize 0.0%
operation.go:66 WithReference 0.0%
Coverage diff with base branch
File Function Base Coverage Current Coverage
Total 47.1% 47.4% (+0.3%)
openapi3/entities.go RequestBodyReferenceEns 0.0% 100.0% (+100.0%)
openapi3/entities.go ResponseReferenceEns 0.0% 100.0% (+100.0%)
openapi3/helper.go SetReference no function 100.0%
openapi3/reflect.go setupRequest 58.3% 64.3% (+6.0%)
openapi3/reflect.go setupResponse 83.3% 84.8% (+1.5%)
openapi31/helper.go SetReference no function 100.0%
openapi31/reflect.go setupRequest 66.7% 71.4% (+4.7%)
openapi31/reflect.go setupResponse 83.3% 84.8% (+1.5%)
operation.go WithCustomize no function 0.0%
operation.go WithReference no function 0.0%

@vearutop vearutop merged commit cdbf0a7 into master Sep 9, 2024
6 checks passed
@vearutop vearutop deleted the customize-ref branch September 9, 2024 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant